• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

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

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

Robbiem01

Member
Joined
Feb 9, 2002
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!
 
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
 
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.
 
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
 
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:
Robbiem01 said:
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
 
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?
 
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 said:
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
 
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
 
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:
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 :rolleyes: , but am constantly trying!) and actually seeing the result....
JigPu
 
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:
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
 
Back