Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can you tell me how to calculate the time which CPU had used?

Example:
----------------------
C#
public int Main()
{

int a, b, c;
a = 1; b = 2;
return (c = a + b);

}

----------------------

If we ignore these time: compiling, provide varibles storage, load data to stack/heap...
Then, how to know the nanoseconds to execute this command by CPU Registers:
C#
return (c = a + b);


Thanks for solutions!
Posted
Updated 4-Sep-14 22:48pm
v2
Comments
Mehdi Gholam 5-Sep-14 4:29am    
Why?
user8x86 5-Sep-14 4:31am    
i don't understand your mean.
Mehdi Gholam 5-Sep-14 4:46am    
Timing a single command makes no sense and has no value by it's self, using timings only makes sense if you have a performance problem.
Dilan Shaminda 5-Sep-14 4:42am    
use Stopwatch Class in .Net to measure execution time
user8x86 5-Sep-14 7:20am    
Thank Dilan. But Stopwatch Class does not give to me the exactly time. I might to wait for loading Stopwatch start, after that, keep waiting for Stopwatch stop.

The addition is one machine instruction, and copying into a buffer is another so 2 clock cycles is the absolute minimum. So - .00000005 seconds (+/- 100%)
 
Share this answer
 
Hi ,
I am not understanding what is your need and why ...
But i can redirect u to Mr.Robert's Article.

Its about ::How To: Measure execution time in C#[^]::
 
Share this answer
 
Comments
user8x86 5-Sep-14 4:45am    
I mean: I wanna know about how long time does CPU Registers take to run the command: return (c = a + b);
You can't. Depends on what speed the CPU is clocked at, what instruction set its running, what the JIT does, whether it gets 'inlined' or branched etc.
 
Share this answer
 
Your mean is: The time - which Microsoft calculates in order to "swap out" any process and "swap in" another process - is static (Can't be changed)?
 
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