c - Which of the following would be more efficient? -


in c:

lets function "myfuny()" has 50 line of codes in other smaller functions called. 1 of following code more efficient?

void myfunction(long *a, long *b); int i; for(i=0;i<8;i++)    myfunction(&a, &b); 

or

myfunction(&a, &b); myfunction(&a, &b); myfunction(&a, &b); myfunction(&a, &b); myfunction(&a, &b); myfunction(&a, &b); myfunction(&a, &b); myfunction(&a, &b);   

any appreciated.

that's premature optimization, shouldn't care...

now, code maintenance point of view first form (with loop) better.

from run-time point of view , if function inline , defined in same compilation unit, , compiler not unroll loop itself, , if code in instruction cache (i don't know moon phases, still believe shouldn't have noticable effect) second 1 may marginally fastest.

as can see, there many conditions fastest, shouldn't that. there many other parameters optimize in program have greater effect code speed one. change affect algorithmic complexity of program have much greater effect. more speaking code change not affect algorithmic complexity premature optimization.

if want sure, measure. on x86 can use kind of trick used in this question accurate measure. trick read processor register count number of cycles spent. question illustrate how code optimization questions can become tricky, simple problems.


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -