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,

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.

>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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.