# Outputting a very small number

• 03-19-2003
VanJay011379
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).
• 03-19-2003
Eibro
What variable type are you storing your final computation in? If you're not already, try using double, or long double. Post some code.
• 03-19-2003
VanJay011379
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;```
• 03-19-2003
DougDbug
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?
• 03-19-2003
XSquared
double(fTime - sTime) / (double)CLOCKS_PER_SEC
• 03-20-2003
DougDbug
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.