473,800 Members | 3,022 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4781
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************ *********@g49g2 000cwa.googlegr oups.com>,
Robert <zh*******@gmai l.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*******@gmai l.com> wrote in message
news:11******** *************@g 49g2000cwa.goog legroups.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
QueryPerformanc eCounter 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*******@gmai l.com> wrote in message
news:11******** *************@g 49g2000cwa.goog legroups.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************ *********@g49g2 000cwa.googlegr oups.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
3410
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 benchmark some function calls for Zope project and when I use t0=time.clock(); foo(); print time.clock()-t0 I get much smaller values than when I use time.clock() (most of them 0.0 but some 0.01) When I use time.time() I get values like...
3
3158
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 tested timer functions capabilities with a short script: import time import os
0
408
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 programm I need to meassure the processing time in ms. Is there perhaps something like an environment variable controlling the precision of clock(), or is there any other way of meassuring time in ANSI C in the ms domain??
3
2530
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 is a great high-res 64-bit performance counter that doesn't roll-over for many (many) years, but I'm worried about how many bits Python uses for it and when it will roll over. I need it to take years to roll over. I'm also aware that the actual...
33
47636
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
454
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, 12, 13, 14... How can I change the t_clock's precision? Code list below: //-----------------
5
7660
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
2421
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 resolution. For hours I've been digging the Visual C++ 6 help and Googling like a madman. It seems to me that the standard C just don't have the tool to get this type on information. Sure I found the time() and the clock() but they don't really...
8
1856
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+ from datetime import * from time import * from visa import *
0
9695
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9555
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10042
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9099
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7588
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6826
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5479
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4156
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 we have to send another system
2
3770
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.