Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 16th, 2005, 9:54 AM   #1
JDStud6
Programmer
 
Join Date: Jan 2005
Location: Charleston, SC www.wareonearth.com
Posts: 57
Rep Power: 4 JDStud6 is on a distinguished road
Send a message via AIM to JDStud6
Darkhack's cpu speed tester

Quote:
- CPU Speed Tester
Have the system do a mathmatical computation like 100! (factorial) and use a timer to see how long it took to run and output the time. To make your application useful on faster systems, run a check to see if the time was under 1/5 of a second to increase the size of the factorial. For example if a fast computer was able to do 100! in under 1/5 of a second, have it do 1000! factorial and report back that time instead. Create your own grading scale based off the times. Try to see if your program can guess how fast the machine is, and report back its guess in Mhz based off the results
Just like he said, I am bored and wanted something to do. I am new to C# but this is what I have:

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
JDStud6 is offline   Reply With Quote
Old Dec 16th, 2005, 11:19 AM   #2
teencoder
Hobbyist Programmer
 
teencoder's Avatar
 
Join Date: Jul 2005
Posts: 158
Rep Power: 0 teencoder is an unknown quantity at this point
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.
teencoder is offline   Reply With Quote
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
Old Dec 16th, 2005, 2:50 PM   #4
Klipt
Hobbyist Programmer
 
Join Date: Dec 2005
Posts: 118
Rep Power: 0 Klipt is an unknown quantity at this point
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; 
        }
Which as a side effect may may your code slower since % is supposed to take quite a few cycles. Then try calculate 10 000! (factorial), 100 000!, 1000 000!... until it takes a noticeable time.

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!)
Klipt is offline   Reply With Quote
Old Dec 16th, 2005, 2:57 PM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Quote:
I wonder if multiplication by 0 is any faster?
I believe that assignment would be faster than multiplication by zero. Just a SWAG, though.
__________________
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
DaWei is offline   Reply With Quote
Old Dec 16th, 2005, 3:05 PM   #6
MBirchmeier
Hobbyist Programmer
 
Join Date: Oct 2005
Posts: 211
Rep Power: 3 MBirchmeier is on a distinguished road
Quote:
Originally Posted by Klipt
public int factorial(int n)
        {
            if (n == 1)
                return 1;
            else
                return (factorial(n - 1) * n)%10; 
        }
Which as a side effect may may your code slower since % is supposed to take quite a few cycles. Then try calculate 10 000! (factorial), 100 000!, 1000 000!... until it takes a noticeable time.
Rough calculations leave this to be about 100 clock cycles / iterations. or about 10000 iterations required for one ms on a 1GHz cpu. (these numbers might be a bit off and i'll spare the disassembly unless someone asks)

Quote:
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!)
It looks like a mulitplication will take no less than 13* clock cycles on this arcitecture. (vs an average of 20 for our estimate) the idiv required for the '%' (43 clock cycles) will help de-weight that effect.

-MBirchmeier

* http://home.comcast.net/~fbui/intel/i.html#imul
MBirchmeier is offline   Reply With Quote
Old Dec 17th, 2005, 6:44 PM   #7
Darkhack
Hobbyist Programmer
 
Darkhack's Avatar
 
Join Date: Dec 2005
Location: Kansas City
Posts: 102
Rep Power: 3 Darkhack is on a distinguished road
Send a message via AIM to Darkhack
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.
Darkhack is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:54 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC