473,511 Members | 16,660 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need help in timing program

ilikepython
844 Recognized Expert Contributor
I wrote a program that generates random numbers. I want to make it so that it gives me the time it took to complete the program. Here's my code:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <ctime>
  3. #include <cstdlib>
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8.     float timer = time(0);
  9.     srand((unsigned)time(0));
  10.     int numofnums = 100;
  11.     int nums[numofnums];
  12.     for (int x = 0; x < numofnums; x++){
  13.         nums[x] = rand()%10 + 1;
  14.         cout << nums[x] << endl;}
  15.     float timed = (unsigned)time(0) - timer;
  16.     cout << "TIME: "<< (unsigned)timed << endl << timer << endl << time(0) << endl; //print to see what is going on
  17.     system("PAUSE");
  18.     return 0;}
  19.  
The problem is that when I run it the "timed" prints out to a big number usually in between 20-100. The "time(0)" at the end works fine as it is a little higher everytime. I think the problem is that the time doesn't update everytime I run the program. What can I do?

I have the Bloodshed DEV compiler.
Thanks in advance.
Mar 21 '07 #1
2 1212
Roonie
99 New Member
i think that since time(0) returns an integer number of seconds, storing it in a float is inappropriate.

i tried this:
Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream>
  3. #include <ctime>
  4. #include <cstdlib>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.     long timer = time(0);
  11.     srand(time(0));
  12.     int numofnums = 100;
  13.     int nums[100];
  14.     for (int x = 0; x < numofnums; x++){
  15.         nums[x] = rand()%10 + 1;
  16.         cout << nums[x] << endl;}
  17.     long timed = time(0) - timer;
  18.     cout << "TIME: "<< (unsigned)timed << endl << timer << endl << time(0) << endl; //print to see what is going on
  19.     system("PAUSE");
  20.     return 0;
  21. }
  22.  
and it seemed to give a more reasonable value for the elapsed time. (zero seconds as opposed to 40-50.)
Mar 21 '07 #2
ilikepython
844 Recognized Expert Contributor
Thank you, now it works just fine.
Mar 21 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

8
6184
by: Victor | last post by:
I need a JavaScript timer - I have five events I need to time, that can be triggered by a mouseclick event, or a keypress event. Each event is separated by only one to two seconds. The first...
10
2124
by: Greg Stark | last post by:
This query is odd, it seems to be taking over a second according to my log_duration logs and according to psql's \timing numbers. However explain analyze says it's running in about a third of a...
5
5928
by: Pushkar Pradhan | last post by:
I've decided to use clock() to time my routines, my code is like this: clk1 = clock(); while (n != 1000000) { mm_2r2c_2r2c_bc(&a, &b, &c); n++; } clk2 = clock(); t2 = time(NULL);...
6
1896
by: Luke Wu | last post by:
Whenever one runs across programs that use the return value from getchar() to read input, it's almost always accepted into an int-defined variable. I've read explanations on this and have always...
1
1412
by: Novice | last post by:
Hi all, I'm at my wit's end on trying to insert some timing code into the server side code that parses the hashed data contained in the hidden field being submitted to the server I've tried...
2
3278
by: Steven D'Aprano | last post by:
The timeit module is ideal for measuring small code snippets; I want to measure large function objects. Because the timeit module takes the code snippet argument as a string, it is quite handy...
0
963
by: CCG | last post by:
A have a web app in vb.net & asp.net. The page has 3 frames. a left frame shows options and never changes. The top right frame has paramter screen or a report. The bottom horizontal frame has ...
24
1780
by: user923005 | last post by:
The purpose is for a shellsort. I am experimenting with the increment for shellsort using a modified version of Pete's driver and this bit of code (shell_ratio is a floating point number for the...
7
1590
by: Garry Freemyer | last post by:
I've researched this question on internet, and I've tried a number of things I found to no avail to solve this problem... I work for a company using Visual Studio 2003 to maintain a website for...
0
7355
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7423
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7081
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7510
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5668
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3225
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1576
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
447
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.