Click here to Skip to main content
15,890,374 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
For illustration purposes, I have a data structure as shown below. What is the fastest Code 1 or Code 2?

C++
struct Array{

// length of array
public: int length;

// pointer to int array
public: int *pData;

// allocate array
public: bool allocate(int size);

// constructor
public: Array(int size){
allocate(size);
}

// default constructor
public: Array(){
length = 0;
pData = 0;
}

public: ~Array(){
if(pData) delete []pData;
}

};


Code 1:
C++
Array array(10000);

for(int i = 0;i < array.length;i++) array.pData[i] = i*2;


Code 2:

C++
Array array(10000);

int l = array.length;

int *pData = array.pData;

for(int i = 0;i < l;i++) pData[i] = i*2;
Posted
Updated 14-Jun-14 2:37am
v2
Comments
[no name] 14-Jun-14 8:31am    
Benchmark your code and find out for yourself.
BupeChombaDerrick 14-Jun-14 8:42am    
Does accessing the member variable within a loop affect the code performance? I'am writing computer vision code and I want to fully optimize it.
Philippe Mori 14-Jun-14 9:05am    
Such optimizations almost never matters and when it matters, you should probably use parallel loops anyway so that all cores would works.

Usually, you select appropriate algorithms (for ex. O(n log n) instead of O(n²)) and then you use profiling tools once you have working code to tune code where it does matters.

Usually it not low-lvel optimizations that matters much but high level optimizations.
BupeChombaDerrick 14-Jun-14 14:52pm    
I have already created my own computer vision algorithms for mobile and I have this app using my vision algorithms https://play.google.com/store/apps/details?id=com.facebook.rethinkvision.Bimostitch, I want to optimize the code on low and high level. You are right I guess I need to do parallel processing, I'am currently doing some research on that topic, any links to good literature on parallel processing can be helpful.
Jochen Arndt 14-Jun-14 9:13am    
There should be no need to optimize the code because this will be done by the compiler. You may let the compiler generate assembly output for both versions and compare them to see which might be faster.

As already suggested, if in doubt, make the compiler generate the assembly codes for you and then compare them. With a decent compiler there should be no difference.
 
Share this answer
 
Comments
BupeChombaDerrick 14-Jun-14 14:58pm    
Thank you. I guess I have to focus more on hardware acceleration.
You could try writing C++ and use std::vector and std::generate(). Only if that's too slow consider managing memory yourself. Even if you need to manage memory yourself then use std::unique_ptr to automagically sort out releasing the memory.
 
Share this answer
 
v3
in a release build both solution should have the same speed, because optimization.

Code 3:

C#
Array array(10000);

int l = array.length;

int *pData = array.pData;

for(int i = 0;i < l;i++) pData++ = i*2;


with offsetting the accessed array
pData++

:-)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900