Notices

Overclockers Forums > Software > Programming Tips and Tricks
Programming Tips and Tricks
Forum Jump

Time - I need to track how long program takes...

Post Reply New Thread Subscribe Search this Thread
 
 
Thread Tools
Old 04-07-02, 08:35 PM Thread Starter   #1
Robbiem01
Member



Join Date: Feb 2002

 
Time - I need to track how long program takes...


How can I track how long something takes to do with .000 percision? I know how to access time (NULL); but that is only accurate up to seconds and if execution takes .8 seconds it just tells me 0. Time (NULL) returns an integer not a double. Anyone know how to time something? Heres how I have it now.

#include <time.h>
#include <iostream.h>

int main () {
int pretime = time (NULL);

//stuff is done here

int posttime = time (NULL);

cout << "that took " << posttime - pretime << " seconds.";
}

This does work but it is only accurate to the second and not to the 1/1000th of a second like I would like. I need help badly!
Robbiem01 is offline   QUOTE Thanks
Old 04-08-02, 02:30 AM   #2
Istari1
Member

 
Istari1's Avatar 

Join Date: Mar 2002
Location: Raleigh NC

 
Just wrap that loop in another loop say thats goes from 1 to 1000 then divide the total time it takes by 1000. This is how you get around the clock accuracy problem. The more times the loop runs the greater accuracy and precision you get.

Josh
Istari1 is offline   QUOTE Thanks
Old 04-08-02, 01:45 PM Thread Starter   #3
Robbiem01
Member



Join Date: Feb 2002

 
I see what you mean but my problem is that the
//stuff is done here
will take about 5 seconds to do and I dont want to make the user sit there for 5000 seconds so I can tell them it took 5.03 seconds to do it once. I need a timer that is double or float or a clock that returns double or float.
Robbiem01 is offline   QUOTE Thanks
Old 04-08-02, 04:43 PM   #4
Istari1
Member

 
Istari1's Avatar 

Join Date: Mar 2002
Location: Raleigh NC

 
Then I am confused. If its taking more then a second you shouldnt even have this issue. The clock returns a time in miliseconds. Hence if it was taking 5 seconds your number would be 5000. And if you got 5030, then it was taking 5.03 seconds. . .so I'm confused. And FYI, the way I mentioned before is the only way around this problem anyway. The computer doesnt keep a float or double time, only a long int.

Josh
Istari1 is offline   QUOTE Thanks
Old 04-08-02, 05:59 PM Thread Starter   #5
Robbiem01
Member



Join Date: Feb 2002

 
no i've seen a program that was accurate to the 100th place. Anyways your solution is horrible for my problem because the user has to sit while the computer does the same calculations 5000 times so he knows how long it would have taken to do once. Although it is a good solution for finding how long it takes to do something if it takes like .001 seconds.

To explain a little more about what my program needs this for let me say that this is a prime number finding program. In a search from 1 to 10000 it finds all 1229 primes in "0" seconds. In a search from 1 to 100,000 it finds all 9592 prime numbers in "8" seconds and from 1 to 1,000,000 it takes about 677 seconds. I would like to get these times more accurate but without running a 3385000 second loop (667 * 5000)

Thanks for your help though. I have learned from it and will probably use that tecneque (i'm a bad speeller) in a future program.

Last edited by Robbiem01; 04-08-02 at 06:34 PM.
Robbiem01 is offline   QUOTE Thanks
Old 04-08-02, 06:29 PM   #6
Istari1
Member

 
Istari1's Avatar 

Join Date: Mar 2002
Location: Raleigh NC

 
Quote:
Originally posted by Robbiem01
no i've seen a program that was accurate to the 100th place. Anyways your solution is horrible because the user has to sit while the computer does the same calculations 5000 times so he knows how long it would have taken to do once.
Shrug, well Robbie tell ya what. When you find a better solution be sure to tell me since mine is so bad. I'm only telling ya what 10 years of programming has taught me. Unless you are willing to also program your own timer in ASM (which I can already tell you isnt anywhere as easy as it would sound). Anyway, please do tell me when you find a better way.

Josh
Istari1 is offline   QUOTE Thanks
Old 04-08-02, 06:38 PM Thread Starter   #7
Robbiem01
Member



Join Date: Feb 2002

 
sorry if I made that post sound like yours was a horrible way. I edited it after re-reading it so it wouldn't sound like that then after I submitted the changes I saw your post. Anyways thanks for trying to help. 10 years you say ... what kind of programming do you do?
Robbiem01 is offline   QUOTE Thanks
Old 04-08-02, 06:57 PM Thread Starter   #8
Robbiem01
Member



Join Date: Feb 2002

 
good news! I found the anwser at www.cplusplus.com

heres a sample program I wrote that deminstrates how to do this.l..

#include <time.h>
#include <iostream.h>

int main ( ) {
int starttime = clock ( );

for (int i = 1; i < 1000000; i++)
int y = 3493943 * 3893883 / 38983 - 39 / 2;

int endtime = clock ( );

double timeelapsed = (double(endtime) - starttime) / CLK_TCK;

cout << "Time Elapsed = " << timeelapsed;
}

sorry about the tabs ... this forum doesn't like tabs and spaces at the beginning of lines so the code looks kinda sloppy.
Robbiem01 is offline   QUOTE Thanks
Old 04-08-02, 06:59 PM   #9
Istari1
Member

 
Istari1's Avatar 

Join Date: Mar 2002
Location: Raleigh NC

 
Quote:
Originally posted by Robbiem01
sorry if I made that post sound like yours was a horrible way. I edited it after re-reading it so it wouldn't sound like that then after I submitted the changes I saw your post. Anyways thanks for trying to help. 10 years you say ... what kind of programming do you do?
Thats Ok, programming sometimes gets me a little angry anyway Mostly Java and some C/C++. Used to use ASM, but its not really worth it these days unless you are doing device drivers and other lower level stuff. I'm also a tutor at NC State University. Well as for your problem you could maybe check the accuracy then decide if the program needs to loop or not. If you are doing numbers from 1-10000 then you are prolly safe just running once, but if you are doing 1-10 then you are going to need to loop a couple hundred times at least. I dont know what to tell ya about the program ya saw with the very high time accuracy, I've seen people write timers in ASM, but they are not even very reliable because they count on the computer running each one as 1 instruction code and then using that time based on the computers computational time to create the timer. With today's instruction set optimizations this approach is less feasible.

Josh
Istari1 is offline   QUOTE Thanks
Old 04-08-02, 07:03 PM   #10
Istari1
Member

 
Istari1's Avatar 

Join Date: Mar 2002
Location: Raleigh NC

 
I may have found something to help you. Check out this ink and see if you may be able to use this timer class. I believe its generic C, but it may be for Visual C only.

http://www.codeguru.com/system/CWaitableTimer.shtml

Hope it helps.

Josh
Istari1 is offline   QUOTE Thanks
Old 04-08-02, 08:35 PM   #11
JigPu
Inactive Pokémon Moderator

 
JigPu's Avatar 

Join Date: Jun 2001
Location: Vancouver, WA

10 Year Badge
 
What are you calculating the primes for? I have written a few prime number programs in BASIC and if you want any tips for speeding up the calculation portion.... Unless that isn't the goal of your project...

JigPu

__________________
.... ASRock Z68 Extreme3 Gen3
.... Intel Core i5 2500 ........................ 4 thread ...... 3300 MHz ......... -0.125 V
2x ASUS GTX 560 Ti ............................... 1 GiB ....... 830 MHz ...... 2004 MHz
.... G.SKILL Sniper Low Voltage ............. 8 GiB ..... 1600 MHz ............ 1.25 V
.... OCZ Vertex 3 ................................. 120 GB ............. nilfs2 ..... Arch Linux
.... Kingwin LZP-550 .............................. 550 W ........ 94% Eff. ....... 80+ Plat
.... Nocuta NH-D14 ................................ 20 dB ..... 0.35 C°/W ................ 7 V


"In order to combat power supply concerns, Nvidia has declared that G80 will be the first graphics card in the world to run entirely off of the souls of dead babies. This will make running the G80 much cheaper for the average end user."
"GeForce 8 Series." Wikipedia, The Free Encyclopedia. 7 Aug 2006, 20:59 UTC. Wikimedia Foundation, Inc. 8 Aug 2006.
JigPu is offline   QUOTE Thanks
Old 04-08-02, 08:48 PM Thread Starter   #12
Robbiem01
Member



Join Date: Feb 2002

 
That wasn't the original goal of this thread but I do want to speed up my program. I am not using the fastest method (sieve.) Here is the main loop of the program.

double pretime = clock ();
//////////////////////////////////////////////////////////////////////////////
/////////////Finds Prime Numbers and outputs while finding them///////////////
//////////////////////////////////////////////////////////////////////////////
int numberofprimes = 0;
for (int number = startnum; number < endnum; number++) {
bool isprime = 0;
// 0 is prime, 1 is not prime. Assumed prime until proven not prime
for (int factor = 2; factor < number / 2 + 1; factor++) {
if (number % factor == 0) {
isprime = 1;
break;//exits loop after number is found to have one integer factor
}//end if
}//end for (int fac...

if (isprime == 0){
numberofprimes++;
if (output_type == 0) {
cout << setw (10) << number;
if (numberofprimes % 5 == 0)
cout << endl;
}
else {//output_type == 1
outs << setw (10) << number;
if (numberofprimes % 10 == 0)
outs << endl;
}
}//end if
}//end for (int nu...

double posttime = clock ();
double timeelapsed = (posttime - pretime) / CLK_TCK;






If you would like to run the program you can download it at http://robbiem01.tripod.com/primehunter.html

Last edited by Robbiem01; 04-08-02 at 08:57 PM.
Robbiem01 is offline   QUOTE Thanks
Old 04-09-02, 08:08 PM   #13
JigPu
Inactive Pokémon Moderator

 
JigPu's Avatar 

Join Date: Jun 2001
Location: Vancouver, WA

10 Year Badge
 
Sorry about the hijack... But was wondering.

Firstly, from what I can decode (as I don't know C or C++), you are checking if the number is divisible by any number up to Number / 2 + 1. Right? If that is the case, you can make you program faster by checking less numbers.

First of all, instead of stopping at Number / 2 + 1, you can stop at Square Root (Number) + 1 instead. A number will be prime if is isn't divisible by any number up to it's square root.

Also, instead of checking ALL the numbers up to the square root, you can just check with only the odd numbers (you might already be doing this... I don't completely understand C as I said). Once again, a number is only prime if it isn't divisible by any of the primes below it. That shaves off about half of your test numbers.

I have one last suggestion, but I think you already have it... Checking for factors using MOD (modulo) to see if it is evenly divisible (the "if (number % factor == 0)" seems to just scream this).

Once again, sorry about the hijack, but I can't stop myself when it comes to primes... Just something about my obsession with writing faster algorithms (have yet to manage to figure out how to program a sieve myself , but am constantly trying!) and actually seeing the result....
JigPu

__________________
.... ASRock Z68 Extreme3 Gen3
.... Intel Core i5 2500 ........................ 4 thread ...... 3300 MHz ......... -0.125 V
2x ASUS GTX 560 Ti ............................... 1 GiB ....... 830 MHz ...... 2004 MHz
.... G.SKILL Sniper Low Voltage ............. 8 GiB ..... 1600 MHz ............ 1.25 V
.... OCZ Vertex 3 ................................. 120 GB ............. nilfs2 ..... Arch Linux
.... Kingwin LZP-550 .............................. 550 W ........ 94% Eff. ....... 80+ Plat
.... Nocuta NH-D14 ................................ 20 dB ..... 0.35 C°/W ................ 7 V


"In order to combat power supply concerns, Nvidia has declared that G80 will be the first graphics card in the world to run entirely off of the souls of dead babies. This will make running the G80 much cheaper for the average end user."
"GeForce 8 Series." Wikipedia, The Free Encyclopedia. 7 Aug 2006, 20:59 UTC. Wikimedia Foundation, Inc. 8 Aug 2006.
JigPu is offline   QUOTE Thanks
Old 04-09-02, 08:30 PM Thread Starter   #14
Robbiem01
Member



Join Date: Feb 2002

 
thanks for the input ... you were right about everything you said. I wasn't checking only odds in that post but sience then I did fix that. I havn't fixed the square root thing so thats good you brought that up. I was using modulus like you thought. You should learn c. You seem very smart and have a programmer's kind of mind. Anyways thanks for the help. I will post my program again later if you want to see all the changes i've made.

Edit:
whoa! I just made the change and that square root thing made my program go from finding 10,000 primes in 4 seconds to finding 10,000 primes in only 0.05 seconds! I dont see how testing 1/2 the numbers made it go about 80 times faster but i'll take it! Maybe square roots are faster then division....

Last edited by Robbiem01; 04-09-02 at 08:40 PM.
Robbiem01 is offline   QUOTE Thanks
Old 04-10-02, 08:56 AM   #15
JigPu
Inactive Pokémon Moderator

 
JigPu's Avatar 

Join Date: Jun 2001
Location: Vancouver, WA

10 Year Badge
 
Well, at 100,000 with the division, you would be checking 25,000 numbers. Throwing in the square root brings that down to an astonishing 125 numbers!!

My own program will do primes to 100,000 in 0.9 seconds, but your (old) program would do it in 8 seconds. That square root makes a big diffrence!!

JigPu

__________________
.... ASRock Z68 Extreme3 Gen3
.... Intel Core i5 2500 ........................ 4 thread ...... 3300 MHz ......... -0.125 V
2x ASUS GTX 560 Ti ............................... 1 GiB ....... 830 MHz ...... 2004 MHz
.... G.SKILL Sniper Low Voltage ............. 8 GiB ..... 1600 MHz ............ 1.25 V
.... OCZ Vertex 3 ................................. 120 GB ............. nilfs2 ..... Arch Linux
.... Kingwin LZP-550 .............................. 550 W ........ 94% Eff. ....... 80+ Plat
.... Nocuta NH-D14 ................................ 20 dB ..... 0.35 C°/W ................ 7 V


"In order to combat power supply concerns, Nvidia has declared that G80 will be the first graphics card in the world to run entirely off of the souls of dead babies. This will make running the G80 much cheaper for the average end user."
"GeForce 8 Series." Wikipedia, The Free Encyclopedia. 7 Aug 2006, 20:59 UTC. Wikimedia Foundation, Inc. 8 Aug 2006.
JigPu is offline   QUOTE Thanks

Post Reply New Thread Subscribe


Overclockers Forums > Software > Programming Tips and Tricks
Programming Tips and Tricks
Forum Jump

Thread Tools Search this Thread
Search this Thread:

Advanced Search


Mobile Skin
All times are GMT -5. The time now is 10:50 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.
You can add these icons by updating your profile information to include your Heatware ID, Benching Profile ID or your Folding/SETI profile ID. Edit your profile!
X

Welcome to Overclockers.com

Create your username to jump into the discussion!

New members like you have made this the best community on the Internet since 1998!


(4 digit year)

Why Join Us?

  • Share experience
  • Max out your hardware
  • Best forum members anywhere
  • Customized forum experience

Already a member?