0

Hello,
I am trying to optimize or make my code faster and I am not so sure how.
So I have two level nested for loop and inside there, I assign value to the array.
For example,

tmp[0]=*ptr;
tmp[0]=100;

I found out that the second one is faster than the first one and I don't know why.
Is there anyway I can optimize the first one to something that could make my program faster?
Thank you so much,

4
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by kes166
1

You are wasting your time and efforts trying to optimize that. There are only a few clock ticks difference between the two, and any optimizing you do with that will not even be noticable.

Concentrate first at the function level -- do some profiling to see where the bottle necks are.

0

>Is there anyway I can optimize the first one to something that could make my program faster?
Your program is slow because your code sucks. Don't make it suck more by trying to micro-optimize.

1

Going through how to optimize code would take months to explain. Linear code is linear code. Both statements take x amount of time. Avoid many nested loops. If you have code, it would be easier to explain where your coding might be inefficient.

Edit:

If you just want to know why, I would think it's because the first line is assigning a value to a variable where the CPU has to look up the value in *ptr where the second line, you are just assigning a value directly to the variable and no other work is needed.

Edited by kes166: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.