View Single Post
Old Dec 16th, 2005, 1:32 PM   #3
MBirchmeier
Hobbyist Programmer
 
Join Date: Oct 2005
Posts: 211
Rep Power: 3 MBirchmeier is on a distinguished road
Just to highlight a point, you're using the factorial function as the main loop for calcualting CPU speed.

Lets look at the disassembly of that loop. (lines without address spaces represent the C# code, )

		public int factorial(int n)
		{
			if (n == 1)
00000000  push        ebp  
00000001  mov         ebp,esp 
00000003  sub         esp,0Ch 
00000006  push        edi  
00000007  push        esi  
00000008  push        ebx  
00000009  mov         dword ptr [ebp-4],ecx 
0000000c  mov         esi,edx 
0000000e  xor         edi,edi 
00000010  cmp         esi,1 
00000013  jne         00000019 
				return n;
00000015  mov         edi,esi 
00000017  jmp         00000030 
				return factorial(n - 1) * n; 
00000019  lea         edx,[esi-1] 
0000001c  mov         ecx,dword ptr [ebp-4] 
0000001f  call        dword ptr ds:[00975A7Ch] 
00000025  mov         ebx,eax 
00000027  mov         eax,esi 
00000029  imul        eax,ebx 
0000002c  mov         edi,eax 
0000002e  jmp         00000030 
		}
00000030  mov         eax,edi 
00000032  pop         ebx  
00000033  pop         esi  
00000034  pop         edi  
00000035  mov         esp,ebp 
00000037  pop         ebp  
00000038  ret

A quick look at our cheat sheet lets us know the majority of these instructions will take one clock cycle with the excpetion of call and ret (about 5) jmp(about 3), and imul (about 20).

That puts this routine at about 50 clock cycles/iteration (it's actually lower, I counted all instructions as being executed each time, where in reality only one branch will be, but it keeps calcualtions easy). At 500 iteration that brings us to a whopping 25000 clock cycles.

Given that your computer is lets just say 250MHz cpu (for the sake of easy calculation)
25,000 cycles / (250,000,000 cycles / s) = 1/10,000 s or .1 ms.

On average to even register a millisecond of calculation you'd need to do 20 iterations of the loop for each MHz of cpu speed you have. So your average 2GHz machne would requre running Factorial(40000), which unfortunately will return an overflow error, before getting this function to run for a millisecond.

-MBirchmeier
PS: Like I said this is a simplification of the calculations necessary. A few of these moves and such take a touch longer... but I believe my calculation of about 50 clock cycles / call is about accurate.
MBirchmeier is offline   Reply With Quote