473,385 Members | 1,769 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,385 software developers and data experts.

change clock's precision

Hello all,

I use clock() to measure the time cost in my program.
But the t_clock always be the multiply of 10, for example 10, 20, 30,
40,...
And I want to get the precision of 1, such as 11, 12, 13, 14...
How can I change the t_clock's precision?

Code list below:
//-----------------
#include <cstdio>
#include <fstream.h>
#include <ctime>

void main() {
int x;
clock_t t_click,t_start;

t_start = clock();
for (x=0;x<60;x++) {
printf("%d\n",x);
}

t_click = clock() - t_start;
printf("Time Elapsed %d\n",t_click);

}
//-------------------
BTW, I use VC6.0. The environment is P3 600MHz and WinXP.

Any suggestions will be appreciated!

Best regards,
Davy

Oct 5 '05 #1
5 4744
Robert wrote:

Hello all,

I use clock() to measure the time cost in my program.
But the t_clock always be the multiply of 10, for example 10, 20, 30,
40,...
And I want to get the precision of 1, such as 11, 12, 13, 14...
How can I change the t_clock's precision?


You can't.
The 'precision' is given to you and there is no way to change
that. That may eg. be because the hardware doesn't support a
finer granularity.

--
Karl Heinz Buchegger
kb******@gascad.at
Oct 5 '05 #2
In article <11*********************@g49g2000cwa.googlegroups. com>,
Robert <zh*******@gmail.com> wrote:
I use clock() to measure the time cost in my program.
But the t_clock always be the multiply of 10, for example 10, 20, 30,
40,...
And I want to get the precision of 1, such as 11, 12, 13, 14...
How can I change the t_clock's precision?
There is no way to do that in standard C. Keep in mind that
clock_t is a count of CLOCKS_PER_SEC and not an absolute time,
so a value of 10 might mean 10 femtoseconds with the extra digit
reserved for the next board rev with the built-in 1 femtosecond
counter.

Code list below:
//-----------------
// comments are not allowed in C89.
#include <cstdio>
#include <fstream.h>
#include <ctime>
Those are not standard C headers. If you expect us to be able to
understand your program in comp.lang.c, you will have to provide the
expansion of the headers.

void main() {
In standards-compliant C code, main() must return an int.

Also, if you are not declaring parameters for main, it is
better practice to prototype it as main(void)
int x;
clock_t t_click,t_start;

t_start = clock();
for (x=0;x<60;x++) {
printf("%d\n",x);
}

t_click = clock() - t_start;
printf("Time Elapsed %d\n",t_click);
You did not divide by CLOCKS_PER_SEC so printing out the number
of ticks gives us no idea how long the program took.
}
In C89, if you do not return a value from main, the exit status
of the program is undefined.
//-------------------
BTW, I use VC6.0. The environment is P3 600MHz and WinXP. Any suggestions will be appreciated!


Any higher precision timing would be implementation specific,
and is thus not a suitable topic for comp.lang.c .
This topic is, by the way, disposed of in the comp.lang.c FAQ...
--
Programming is what happens while you're busy making other plans.
Oct 5 '05 #3
"Robert" <zh*******@gmail.com> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...
I use clock() to measure the time cost in my program.
But the t_clock always be the multiply of 10, for example 10, 20, 30,
40,...
And I want to get the precision of 1, such as 11, 12, 13, 14...
How can I change the t_clock's precision?
You cannot change the precision; you can only measure. Windows allocates
each thread a certain amount of time slice. Apparently the granularity of
that slice on your system is 10/CLOCKS_PER_SEC seconds.

Use a different timing function.

[It is completely off-topic on this group, and my Windows programming skills
are very rusty, but you may start reading on GetTickCount and
QueryPerformanceCounter functions.]
Code list below:
//-----------------
#include <cstdio>
#include <fstream.h>
<fstream.h> is pre-standard; use <fstream> instead. (Though you don't use it
in the program.)
#include <ctime>

void main() {
'void main' is not standard; should be 'int main'
int x;
clock_t t_click,t_start;

t_start = clock();
for (x=0;x<60;x++) {
printf("%d\n",x);
}

t_click = clock() - t_start;
printf("Time Elapsed %d\n",t_click);
This is one example that demonstrates the superiority of C++'s operator<<
overloading over printf. Since we don't know the actual type of clock_t, we
cannot know what format identifier to use with printf (%d didn't match
clock_t on my system).

This works:

cout << "Time Elapsed: " << t_click << '\n';
}
//-------------------
BTW, I use VC6.0.


In that case, in addition to using 'int main', you will have to explicitly
add a return statement at the end of your main function:

return 0;

That statement is not necessary and most other compilers get that right; but
VC++60 needs you to type it. In other words, even though the following is a
valid C++ program, VC++60 will need you to add a return statement.

int main()
{}

Ali

Oct 5 '05 #4
"Robert" <zh*******@gmail.com> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...
Hello all,

I use clock() to measure the time cost in my program.
But the t_clock always be the multiply of 10, for example 10, 20, 30,
40,...
And I want to get the precision of 1, such as 11, 12, 13, 14...
How can I change the t_clock's precision?


divide it by 10 ;)

Oct 5 '05 #5
Hello,

Under Windows, maybe you can get advantage of using timeGetTime() /
timeBeginPeriod() / timeEndPeriod() instead of clock()

Hope this helps

RamOn
In article <11*********************@g49g2000cwa.googlegroups. com>,
zh*******@gmail.com says...
Hello all,

I use clock() to measure the time cost in my program.
But the t_clock always be the multiply of 10, for example 10, 20, 30,
40,...
And I want to get the precision of 1, such as 11, 12, 13, 14...
How can I change the t_clock's precision?

Code list below:
//-----------------
#include <cstdio>
#include <fstream.h>
#include <ctime>

void main() {
int x;
clock_t t_click,t_start;

t_start = clock();
for (x=0;x<60;x++) {
printf("%d\n",x);
}

t_click = clock() - t_start;
printf("Time Elapsed %d\n",t_click);

}
//-------------------
BTW, I use VC6.0. The environment is P3 600MHz and WinXP.

Any suggestions will be appreciated!

Best regards,
Davy

Oct 5 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: peterbe | last post by:
What's the difference between time.clock() and time.time() (and please don't say clock() is the CPU clock and time() is the actual time because that doesn't help me at all :) I'm trying to...
3
by: Szabolcs Nagy | last post by:
I have to measure the time of a while loop, but with time.clock i always get 0.0s, although python manual sais: "this is the function to use for benchmarking Python or timing algorithms" So i...
0
by: Peger, Daniel H. | last post by:
Hi, I'm having a slight problem with the precision of the standard c++ clock function as it is defined in time.h . On my system the shortest meassurable time period is 0.01 seconds, but for my...
3
by: Russell Warren | last post by:
Does anyone know how long it takes for time.clock() to roll over under win32? I'm aware that it uses QueryPerformanceCounter under win32... when I've used this in the past (other languages) it...
33
by: Pushkar Pradhan | last post by:
I'm using clock() to time parts of my code e.g. clk1 = clock(); /* code */ clk2 = clock(); /* calculate time in secs */ ...... clk1 = clock(); /* code */ clk2 = clock();
5
by: Robert | last post by:
Hello all, I use clock() to measure the time cost in my program. But the t_clock always be the multiply of 10, for example 10, 20, 30, 40,... And I want to get the precision of 1, such as 11,...
5
by: Tobiah | last post by:
The manual says: On Unix, return the current processor time as a floating point number expressed in seconds. So I ran this program: #!/usr/bin/python import time
5
by: Hunter | last post by:
Hi all, I know it may sound like dump newbie question (which is very much true, as I am a newbie, not even a real programmer), but I need to implement a calendar time clock with a millisecond...
8
by: Theo v. Werkhoven | last post by:
hi, In this code I read out an instrument during a user determined period, and save the relative time of the sample (since the start of the test) and the readback value in a csv file. #v+...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...

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.