![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 | |
|
Programmer
|
Darkhack's cpu speed tester
Quote:
public int factorial(int n)
{
if (n == 1)
return n;
else
return factorial(n - 1) * n;
}
private void Button1_Click(object sender, EventArgs e)
{
int start = DateTime.Now.Millisecond;
label1.Text = (factorial(Convert.ToInt32(textBox1.Text))).ToString();
int end = DateTime.Now.Millisecond;
label3.Text = ("start: " + start.ToString() + " end: " + end.ToString());
}Would this be right? It always says the start and end time are the same, so how can I fix that? JD |
|
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Jul 2005
Posts: 158
Rep Power: 0
![]() |
Your code could be bad or your computer could be doing it that fast. It doesn't really even take a second for a modern computer to count to a million.
__________________
Geeks may not be cool now but in the long run they prosper. |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Oct 2005
Posts: 211
Rep Power: 3
![]() |
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 retA 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. |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Dec 2005
Posts: 118
Rep Power: 0
![]() |
To prevent overflow, you could return the last digit of the factorial, i.e. use
public int factorial(int n)
{
if (n == 1)
return 1;
else
return (factorial(n - 1) * n)%10;
}EDIT: of course, since the last digit of n! is 0 for any n >= 5 ... I wonder if multiplication by 0 is any faster? In which case, maybe calculate the last digit of the nth Fibonacci number instead (but don't do it recursively!) |
|
|
|
|
|
#5 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Quote:
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
|
#6 | ||
|
Hobbyist Programmer
Join Date: Oct 2005
Posts: 211
Rep Power: 3
![]() |
Quote:
Quote:
-MBirchmeier * http://home.comcast.net/~fbui/intel/i.html#imul |
||
|
|
|
|
|
#7 |
|
Hobbyist Programmer
|
Great job so far! I think its great to see someone trying out some of those challenges. I actually got the idea from hackthissite.org which used to have a thread of programming challenges before the post was deleted by a moderater gone crazy.
Also, just so people know... I did NOT create that program. Infact I havn't even made any of those programs in the challenge list. I don't want people to see my name in the subject line and think I created this. Very well done so far! I'll be sure to try it out once all the kinks are worked out. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|