473,396 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to display elapsed time in seconds (C program)?

9
i wrote a program w/c requires the user to answer addition drills.
my problem is i have to display the time it took the user to answer the drills.
Any codes to suggest?
Help would be much appreciated!
Happy new Year!
Dec 31 '09 #1

✓ answered by mac11

You can grab the current time using the function 'time()' (see http://www.cplusplus.com/reference/clibrary/ctime/time/ for reference)

I'd use it as follows:

Expand|Select|Wrap|Line Numbers
  1. time_t start = 0;
  2. time_t end = 0;
  3. time_t elapsed = 0;
  4.  
  5. start = time(NULL);  // grab start time
  6. // show the question
  7.  
  8. while(questions_not_done); // do whatever until the questions are all over
  9.  
  10. end = time(NULL);  // grab the end time
  11. elapsed = start - end;  // compute elapsed time
  12. // now show the elapsed time

4 26163
mac11
256 100+
You can grab the current time using the function 'time()' (see http://www.cplusplus.com/reference/clibrary/ctime/time/ for reference)

I'd use it as follows:

Expand|Select|Wrap|Line Numbers
  1. time_t start = 0;
  2. time_t end = 0;
  3. time_t elapsed = 0;
  4.  
  5. start = time(NULL);  // grab start time
  6. // show the question
  7.  
  8. while(questions_not_done); // do whatever until the questions are all over
  9.  
  10. end = time(NULL);  // grab the end time
  11. elapsed = start - end;  // compute elapsed time
  12. // now show the elapsed time
Dec 31 '09 #2
Airslash
221 100+
my trick only works in the Windows Operating system as far as I know, but I always use the ::GetTickCount() whenever I need to monitor timeframes between actions.

This function get's the amount of cpu ticks that have elapsed and can be used to express time intervals in miliseconds whereas time() can only do it till seconds.

Expand|Select|Wrap|Line Numbers
  1. #include <windows.h>
  2.  
  3. // some code
  4. int start_point = ::GetTickCount();
  5. // do whatever work here that needs to be monitored
  6. int elapsed_time = ::GetTickCount() - start_point;
  7.  
Dec 31 '09 #3
rhen18
9
thanks! that worked!
i modified some things though.
like i equated elapsed to end - start instead of start - end.
and i declared the variable elapsed as int because otherwise i get a warning when i compile.
other than that, it worked! so thanks very much!
Dec 31 '09 #4
mac11
256 100+
end - start instead of start - end.
Ha ha, what a silly little mistake I made. Good catch!
Jan 4 '10 #5

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

Similar topics

1
by: NotGiven | last post by:
Below is a good elapsed time function I found. However, I'd like to return total seconds instead of broken down into days, hours, minutes & seconds. In other words, I want "125" instead of "2...
3
by: fred14214 | last post by:
I need to be able to enter an elapsed time into a text field. This time then needs to be converted into seconds and stored in a field in some table. The user will enter a time (format is...
5
by: techspirit | last post by:
Hi, I am seeking help for calculating the elapsed time on an activity. When the user clicks "pause" button on a form, the timer should also pause. When the user clicks "continue", the timer...
0
by: Anurag | last post by:
Hi, ENV: DB2 ESE 8.2.3 DPF (11 nodes) on AIX 5.x ==== SCENARIO / SETUP ======== ====== (1) I needed to find out CPU Time (User / System) of SQL queries for benchmark testing. (2) I setup...
3
by: Bill Nguyen | last post by:
I need to display elapsed time for a process in VB.NET in secods. What's the routine to do this? Thanks Bill
1
by: Bill | last post by:
I have a large number of records that have an elapsed time in seconds for each one that I was to have a total time spent. I can sum them up with query easily enough but I need to be able to display...
12
by: Spitfire | last post by:
I've a requirement to find the elapsed time between two function calls. I need to find the time elapsed accurate to 1 millisecond. The problem I'm facing right now is that, I'm using the 'time()'...
0
by: Gabriel Genellina | last post by:
En Fri, 02 May 2008 16:13:41 -0300, Simon Pickles <sipickles@googlemail.comescribió: Two options: a) You can construct a datetime object with that info, using...
4
yarbrough40
by: yarbrough40 | last post by:
Can anyone help me to display a Decimal (or double) to elapsed time :mm:ss (hours:minutes:seconds)using vb.net? example: if you were to type "1.34666666666667" into a cell in Excel then choose...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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
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...
0
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
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,...

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.