GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-Nov-2008, 16:55
lance.kimbrough lance.kimbrough is offline
New Member
 
Join Date: Oct 2008
Posts: 9
lance.kimbrough is on a distinguished road

Strange clock behavior when using GMP


I have just started using GNU MP (the bignum library), and I've been experiencing some strange behaviour when using the clock. I set up a quick program to calculate the time it takes to calculate every factorial from i=1 to 500, and it runs quite quickly (about 3 seconds), however, it always returns 0 elapsed time. My code is...

CPP / C++ / C Code:
#include <time.h>
#include <math.h>
#include <iostream>
#include <gmpxx.h>

int main() {

	clock_t start = clock();       // get program start time

	mpz_class factorialresult;     // declare new mpz (integer) class, and new "factorialresult" variable 
	unsigned long i;               // loop variable
	
	for (i=1; i <= 500; i++)       // for loop to perform factorial on i from i=1 to 500
	{
		mpz_fac_ui (factorialresult.get_mpz_t(), i);   // perform factorial on i using GMP factorial function
		std::cout << factorialresult << " = " << i << "!" <<"\n";  //output result
	}
	
	clock_t end = clock();         // get program end time
	double time_elapsed = double(end-start)/CLOCKS_PER_SEC;  // compute elapsed time
	std::cout << "clocks per sec: " <<CLOCKS_PER_SEC <<"\n";  // display info...
	std::cout << "start: " <<start << "   end: " <<end <<"\n";
	std::cout << time_elapsed << "\n";

	return 0;

}


When I run it, I get (just the last few lines)...
....0692636533817505547812864000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 000 = 500!
clocks per sec: 1000000
start: 0 end: 0


If I use the code on another program that doesn't use GMP, everything seems to work fine. Does anyone know why I would be getting 0 for my start and end time? What am I missing here? Is there something in particular that I must do to get this to work when using the GMP library?

Also, to compile, I execute: g++ factorialtest.cpp -lgmpxx -lgmp

Thanks for the help!
  #2  
Old 02-Nov-2008, 17:53
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: strange clock behavior when using GMP


Quote:
Originally Posted by lance.kimbrough
...strange behaviour when using the clock...
The clock() function, if implemented properly, shows system time of the process, not elapsed time. (The 3 seconds elapsed time is, undoubtedly, due to the printout, which doesn't count against process time, since the cpu can be doing other stuff while it's waiting for the output to happen.)


Bottom line: do you really want elapsed time? Or do you want process time?


What operating system/compiler are you using?

If you want elapsed time:

If it's GNU/Linux with gcc, do a "man gettimeofday" to see how to measure elapsed time.

Or just execute "time progname" from a command line and you will see system time and elapsed time.

To get a significant amount system time, you might do something like calculate 10000 factorial (maybe more if your system is really fast).

Use gettimeofday() before and after the calculation (but before the printout). Compare with the command line "time progname," and compare with your clock() method

Regards,

Dave
  #3  
Old 02-Nov-2008, 20:56
lance.kimbrough lance.kimbrough is offline
New Member
 
Join Date: Oct 2008
Posts: 9
lance.kimbrough is on a distinguished road

Re: Strange clock behavior when using GMP


Thanks for the help Dave! It makes a lot more sense now... I didn't realize how much longer it took to display the result than it took to actually calculate it. I've changed the code up a bit (so now it only calculates a single factorial), so here it is now...

CPP / C++ / C Code:
#include <math.h>
#include <iostream>
#include <ctime>
#include <gmpxx.h>
#include <sys/time.h>

int main() {
	
	timeval time_tod;
	gettimeofday(&time_tod, NULL);      // get start time using gettimeofday
	double t1=time_tod.tv_sec+(time_tod.tv_usec/1000000.0);
	clock_t start = clock();       // get program start time using clock

	mpz_class factorialresult;     // declare new mpz (integer) class
	unsigned long i=200000;        // number to perform factorial

	mpz_fac_ui (factorialresult.get_mpz_t(), i);   // perform factorial on i
	
	gettimeofday(&time_tod, NULL); 
	double t2=time_tod.tv_sec+(time_tod.tv_usec/1000000.0);
	
	std::cout << factorialresult << " = " << i << "!" <<"\n";  //output factorial result
	
	clock_t end = clock();         // get program end time using clock
	
	double time_elapsed = double(end-start)/CLOCKS_PER_SEC;  // compute elapsed time using clock
	double time_elapsed_timeofday = t2-t1;   // compute elapsed time using "gettimeofday"
	
	std::cout << "clocks per sec: " <<CLOCKS_PER_SEC <<"\n";  
	std::cout << "start in clock: " <<start << "   end in clock: " <<end <<"\n";
	std::cout << "Time from clock():  "<< time_elapsed << "\n";
	std::cout << "Time from ""gettimeofday()"": " << time_elapsed_timeofday <<"\n";

	return 0;

}

I have the clock() calculating total time (including time to display the factorial), and the gettimeofday() calculating only calculation time. Here are the results for calculating 200000!, including the return from using "time progname".


....0000000 = 200000!
clocks per sec: 1000000
start in clock: 0 end in clock: 1140000
Time from clock(): 1.14
Time from gettimeofday(): 0.252494

real 0m3.957s
user 0m1.144s
sys 0m0.009s


It's interesting that the clock() calculated time is close to the "user" time, but the gettimeofday() isn't represented in any of the numbers returned from executing "time progname". I wonder what the "sys" time means? Thanks again for the help!
  #4  
Old 02-Nov-2008, 21:00
lance.kimbrough lance.kimbrough is offline
New Member
 
Join Date: Oct 2008
Posts: 9
lance.kimbrough is on a distinguished road

Re: Strange clock behavior when using GMP


I just removed the part of the program that displays the factorial result, and things seem to line up much better (in terms of time). This is what I get with no display of the factorial....


clocks per sec: 1000000
start in clock: 0 end in clock: 240000
Time from clock(): 0.24
Time from gettimeofday(): 0.249288

real 0m0.252s
user 0m0.246s
sys 0m0.006s

...interesting
 
 

Recent GIDBlogToyota - 2009 May Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Click Ticks dayrinni C Programming Language 1 20-Feb-2006 12:50
Strange C++ code memory leakage problem gaoanyu C++ Forum 7 04-Nov-2005 09:09
FLTK clock box widget cable_guy_67 FLTK Forum 0 09-Jul-2005 19:51
Computer Clock Problem kselin Computer Hardware Forum 7 21-Jun-2005 09:26
Strange behaviour in printf (interesting) nusstu C Programming Language 9 05-Apr-2004 14:36

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 21:28.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.