Outputting a very small number

This is a discussion on Outputting a very small number within the C++ Programming forums, part of the General Programming Boards category; Hello, I am attempting to time several processes in my C++ program. I am using the <time.h> library with the ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    59

    Outputting a very small number

    Hello,

    I am attempting to time several processes in my C++ program.

    I am using the <time.h> library with the clock_t = clock() function. I am dividing the start and finish times by CLOCKS_PER_SEC.

    However, in many cases the time is less than a second and I cannnot determine how to output this time without it being 0.

    I have divided the resulting time by 1000 to produce milliseconds but this produces 0 as well.

    With the <iomanip.h> library, I am able to produce decimal numbers up to about 20 places of precision after the 0. Even then I still get 0.000000 etc.

    How would I explore the value of this extremely small number? How could I produce more decimal places? It appears to have a certain limit (about 20 places).
    " . . . and I lay awake, big dreamers never sleep." - David Lee Roth

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    What variable type are you storing your final computation in? If you're not already, try using double, or long double. Post some code.

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    59
    I'm presently using double. I'll try long double as well.


    I have snippets of my code below:
    Code:
    clock_t sTime, fTime;	//begin a timer
    sTime = clock();
    
    ...
    ...
    ...
    
    fTime = clock();	//stop the clock and compute the time taken
    
    cout << "started at " << sTime << " and ended at " << fTime << endl;
    cout << "duration was " << double(fTime - sTime) / CLOCKS_PER_SEC << endl;
    double totalTime = double(fTime - sTime) / CLOCKS_PER_SEC;
    " . . . and I lay awake, big dreamers never sleep." - David Lee Roth

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    MULTIPLY!

    To get milliseconds, you want to MULTIPLY x 1000. 10 sec x 1000 = 10,000 miliseconds.

    What happens when you leave-off the divide?

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    double(fTime - sTime) / (double)CLOCKS_PER_SEC
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    20 decimal places???

    The smallest number you can get is 1/CLOCKS_PER_SECOND.
    That's usually 0.001, or 1 millisecond. If your time difference is less than one count (one millisecond), the result will be zero because the counts are whole numbers.

    If you're running windows, the actual resolution may even be less! With Windows 98, clock() only gets updated about 18 times per second (every 55mS). The result is reported in mS, but it only gets updated every 55mS. As far as I know, the newer versions of windows have the same limitation.

    If I run a tight loop, my results look something like this:
    0
    0
    0
    55
    55
    55
    110
    110
    110

    GetTickCount() does seem to get updated every single millisecond with windows.

    Assuming you're using a PC; To get times less than 1mS, you might have to use the Programmable Interval Timer (PIT).... I'm really not sure... and I don't know exactly how to do it. Using the PIT is sort-of driver-like. If you're using Win98, you can access it. With XP, you have to write a kernel mode driver to directly access the hardware.
    Last edited by DougDbug; 03-20-2003 at 12:48 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Learning Memory, Ins and Outs?
    By Zoiked in forum C Programming
    Replies: 1
    Last Post: 08-27-2007, 04:43 PM
  2. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  3. Perfect number...
    By Argo_Jeude in forum C++ Programming
    Replies: 8
    Last Post: 07-12-2005, 01:53 PM
  4. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  5. help with a source code..
    By venom424 in forum C++ Programming
    Replies: 8
    Last Post: 05-21-2004, 12:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21