c clock() in different OS - c

i need a 60HZ timer (16.6 ms trigger once)
it work well in windows(mingw gcc) but not in liunx(gcc)
can anyone help me abust this? THX
#include <stdio.h>
#include <time.h>t
#define PRE_MS CLOCKS_PER_SEC / 1000
int main()
{
clock_t pre = clock();
int cnt = 0;
printf("CLOCKS_PER_SEC = %d\n", CLOCKS_PER_SEC);
while (1)
{
clock_t diff = clock() - pre;
if (diff > 16 * PRE_MS)
{
cnt++;
if (cnt > 60)
{
printf("%d\n", (int)pre);
cnt = 0;
}
pre += diff;
}
}
}
printf pre 1s in windows
CLOCKS_PER_SEC = 1000
1020
2058
3095
4132
5169
6206
7243
8280
9317
printf pre 2s in linux
CLOCKS_PER_SEC = 1000000
1875000
3781250
5687500
7593750
9500000
11406250
13312500
15218750

First a misconception: 60 Hz is not 17 operations per second but 60.
Second the period check is reading clock() twice and discarding any time interval for printf() to be called. AFAIK the CLOCKS_PER_SEC is larger on Linux than Windows systems, so there is more chance that you are 'throwing away' clock ticks. Read the clock() once, for example:
#include <stdio.h>
#include <time.h>
int main(void)
{
unsigned long long tickcount = 0;
clock_t baseticks = clock();
while (tickcount < 180) { // for 3 seconds
tickcount++;
clock_t nexttick = (clock_t) (baseticks + tickcount * CLOCKS_PER_SEC / 60);
while(clock() < nexttick) {} // wait
printf("Tick %llu\n", tickcount);
}
return 0;
}
The code works from the total elapsed time, so any intervals that are not an exact number of clock ticks are averaged out (instead of a cumulative rounding-off error).
At some point the value from clock() will overflow/wrap, so a real implementation that runs for any length of time will have to take care of this.

Related

To display the time in Xcode

I created a simple game in C in Xcode, but time does not elapse. Does anyone know how to solve this problem?
int main(void)
{
long startTime = 0;
long totalTime = 0;
long prevtime = 0;
int num;
init();
cursor = arrayFish;
startTime = clock();
while (1)
{
printFishes();
printf("\n Which fishing port would you like to water? ");
scanf("%d", &num);
printf("\n");
if (num > 6 || num < 1)
{
printf("Please re-enter.\n");
continue;
}
}
totalTime = (clock() - startTime) / CLOCKS_PER_SEC;
printf("Total Elapsed Time: %ld\n", totalTime);
}
It works except for the time mark. So I just wrote the code needed. Thank you.
You need to know Time Stamp Counter register your clock function is reading. If it is a 32 bit register it will reset very frequently.
For example if your CPU has 2GHz clock then 1 sec = 2 * 1e9 cycles and 32 bit unsigned register can count from 0 to 2^32 - 1 cycles. Which is equal to (2^32)/(2*1e9) = 2.14748365 seconds.
In this case your output will always be limited to 2.14 seconds

Is accessing a variable created using malloc inside a loop faster than a locally declared variable?

I have an application that is on a loop and uses a variable. Basically it just copies a string into the variable processes it and then move to the next string. I was wondering how should I declare the variable that I need to use so I wrote this code to test which one would be faster. Its interesting to see that malloc is faster than the variable that I declared locally. I have also thrown in calloc and its slower since its probably zeroing the memory.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <sys/resource.h>
struct rusage ruse;
#define CPU_TIME (getrusage(RUSAGE_SELF,&ruse), ruse.ru_utime.tv_sec + ruse.ru_stime.tv_sec + 1e-6 * (ruse.ru_utime.tv_usec + ruse.ru_stime.tv_usec))
void gen_random_letters(char *random_buffer, const int len)
{
int i;
static const char alphanum[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
/* initialize random seed: */
srand (time(NULL));
for ( i = 0; i < len; ++i) {
random_buffer[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
}
random_buffer[len] = 0;
}
int main()
{
clock_t tic;
clock_t toc;
int counter;
char *buffer_calloc;
char *buffer_malloc;
char buffer_local_variable[1450];
char copy_this[1450];
double time_spent;
double first, second;
int loop_max;
loop_max = 5000000;
gen_random_letters(copy_this, sizeof(copy_this));
/* Try the locally declared variable */
tic = clock();
first = CPU_TIME;
for ( counter = 0; counter <= loop_max; counter++ )
{
//memset(buffer_local_variable,0,sizeof(buffer_local_variable));
memcpy(buffer_local_variable,copy_this,sizeof(buffer_local_variable));
}
toc = clock();
second = CPU_TIME;
time_spent = (toc - tic) / CLOCKS_PER_SEC;
printf("cpu local_variable : %.2f secs\n", second - first);
printf("Elapsed local_variable: %f seconds\n\n", time_spent);
/* Try calloc */
tic = clock();
first = CPU_TIME;
for ( counter = 0; counter <= loop_max; counter++ ){
buffer_calloc = calloc(1450,sizeof(char*));
memcpy(buffer_calloc,copy_this,sizeof(buffer_calloc));
free(buffer_calloc);
}
toc = clock();
second = CPU_TIME;
time_spent = (toc - tic) / CLOCKS_PER_SEC;
printf("cpu calloc : %.2f secs\n", second - first);
printf("Elapsed calloc : %f seconds\n\n", time_spent);
/* And now malloc */
tic = clock();
first = CPU_TIME;
for ( counter = 0; counter <= loop_max; counter++ ){
buffer_malloc = malloc(1450 * sizeof(char*));
memcpy(buffer_malloc,copy_this,sizeof(buffer_malloc));
free(buffer_malloc);
}
toc = clock();
second = CPU_TIME;
time_spent = (toc - tic) / CLOCKS_PER_SEC;
printf("Cpu malloc : %.2f secs\n", second - first);
printf("Elapsed malloc : %f seconds\n", time_spent);
return 0;
}
Result:
cpu local_variable : 0.57 secs
Elapsed local_variable : 0.000000 seconds
cpu calloc : 2.08 secs
Elapsed calloc : 2.000000 seconds
Cpu malloc : 0.39 secs
Elapsed malloc : 0.000000 seconds
I was expecting the locally declared variable to be faster since the memory for it is already allocated unlike the malloc where it needs to be called every loop. Is my code flawed that is why malloc is faster or that is just the way it is.
Your code copies the wrong number of bytes in the calloc and malloc cases. sizeof(buffer_malloc) gives you the size of the pointer.
Try using 1450 instead of sizeof(...) for those cases.
Results on my laptop (2015 Macbook) with the above change:
cpu local_variable : 0.16 secs
Elapsed local_variable: 0.000000 seconds
cpu calloc : 1.60 secs
Elapsed calloc : 1.000000 seconds
Cpu malloc : 0.56 secs
Elapsed malloc : 0.000000 seconds
UPDATE
You're also allocating 1450 * sizeof(char*) bytes with malloc, when you should really be using 1450 * sizeof(char).
After that fix, the results get a little closer:
cpu local_variable : 0.16 secs
Elapsed local_variable: 0.000000 seconds
cpu calloc : 0.76 secs
Elapsed calloc : 0.000000 seconds
Cpu malloc : 0.57 secs
Elapsed malloc : 0.000000 seconds

Using time() and clock() in C Problems

I'm working on a programming assignment and I'm getting strange results.
The idea is to calculate the number of processor ticks and time taken to run the algorithm.
Usually the code runs so quickly that the time taken is 0 sec, but I noticed that the number of processor ticks was 0 at the start and at the finish, resulting in 0 processor ticks taken.
I added a delay using usleep so that the time taken was non-zero, but the processor ticks is still zero and the calculation between the time stamps is still zero.
I've been banging my head on this for several days now and can't get past this problem, any suggestions are extremely welcome.
My code is below:
/* This program takes an input "n". If n is even it divides n by 2
* If n is odd, it multiples n by 3 and adds 1. Each time through the loop
* it iterates a counter.
* It continues until n is 1
*
* This program will compute the time taken to perform the above algorithm
*/
#include <stdio.h>
#include <time.h>
void delay(int);
int main(void) {
int n, i = 0;
time_t start, finish, duration;
clock_t startTicks, finishTicks, diffTicks;
printf("Clocks per sec = %d\n", CLOCKS_PER_SEC);
printf("Enter an integer: ");
scanf("%d", &n); // read value from keyboard
time(&start); // record start time in ticks
startTicks = clock();
printf("Start Clock = %s\n", ctime(&start));
printf("Start Processor Ticks = %d\n", startTicks);
while (n != 1) { // continues until n=1
i++; // increment counter
printf("iterations =%d\t", i); // display counter iterations
if (n % 2) { // if n is odd, n=3n+1
printf("Input n is odd!\t\t");
n = (n * 3) + 1;
printf("Output n = %d\n", n);
delay(1000000);
} else { //if n is even, n=n/2
printf("Input n is even!\t");
n = n / 2;
printf("Output n = %d\n", n);
delay(1000000);
}
}
printf("n=%d\n", n);
time(&finish); // record finish time in ticks
finishTicks = clock();
printf("Stop time = %s\n", ctime(&finish));
printf("Stop Processor Ticks = %d\n", finishTicks);
duration = difftime(finish, start); // compute difference in time
diffTicks = finishTicks - startTicks;
printf("Time elapsed = %2.4f seconds\n", duration);
printf("Processor ticks elapsed = %d\n", diffTicks);
return (n);
}
void delay(int us) {
usleep(us);
}
EDIT: So after researching further, I discovered that usleep() won't affect the program running time, so I wrote a delay function in asm. Now I am getting a value for processor ticks, but I am still getting zero sec taken to run the algorithm.
void delay(int us) {
for (int i = 0; i < us; i++) {
__asm__("nop");
}
}
You can calculate the elapsed time using the below formula.
double timeDiff = (double)(EndTime - StartTime) / CLOCKS_PER_SEC.
Here is the dummy code.
void CalculateTime(clock_t startTime, clock_t endTime)
{
clock_t diffTime = endTime - startTime;
printf("Processor time elapsed = %lf\n", (double)diffTime /CLOCKS_PER_SEC);
}
Hope this helps.
You are trying to time an implementation of Goldbach's Conjecture. I don't see how you can hope to get a meaningful execution time when it contains delays. Another problem is the granularity of clock() results, as shown by the value of CLOCKS_PER_SEC.
It is even more difficult trying to use time() which has a resolution of 1 second.
The way to do it is to compute a large number of values. This prints only 10 of them, to ensure the calculations are not optimised out, but not to distort the calculation time too much.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SAMPLES 100000
int main(void) {
int i, j, n;
double duration;
clock_t startTicks = clock();
for(j=2; j<SAMPLES; j++) {
n = j; // starting number
i = 0; // iterations
while(n != 1) {
if (n % 2){ // if n is odd, n=3n+1
n = n * 3 + 1;
}
else { // if n is even, n=n/2
n = n / 2;
}
i++;
}
if(j % (SAMPLES/10) == 0) // print 10 results only
printf ("%d had %d iterations\n", j, i);
}
duration = ((double)clock() - startTicks) / CLOCKS_PER_SEC;
printf("\nDuration: %f seconds\n", duration);
return 0;
}
Program output:
10000 had 29 iterations
20000 had 30 iterations
30000 had 178 iterations
40000 had 31 iterations
50000 had 127 iterations
60000 had 179 iterations
70000 had 81 iterations
80000 had 32 iterations
90000 had 164 iterations
Duration: 0.090000 seconds

How to calculate clock ticks of only the current process (including all thread)

All is in the title.
In time.h :
The C library function clock_t clock(void)returns the number of clock
ticks elapsed since the program was launched
But for the current process or all process of the OS ?
Thx.
Thx isedev.
This is actually the approximated clock ticks of the current process.
clock_t t;
t = clock();
printf ("Calculating...\n");
float u = 0;
for(int i = 0 ; i < 1000000 ; i++)
u += sqrt(i);
printf("%f", u);
t = clock() - t;
printf ("%d clock ticks elapsed\n%f ms elapsed.\n", t, ((float) t)/CLOCKS_PER_SEC/1000);

time measurement total time vs cpu time

I wrote a sample program to understand the time measurement in C.Below is a small self contained example.I have a function do_primes() that calculates prime numbers.In the main() function between timing code I call do_primes() and also sleep for 20 milliseconds.I am measure time using struct timeval (which I understand returns clock time.) and also cpu_time using CLOCKS_PER_SEC.Now as I understand it,this denotes the time for which the CPU was working.
The output of the program is as follows.
Calculated 9592 primes.
elapsed time 2.866976 sec.
cpu time used 2.840000 secs.
As you can see the differnece between the elapsed time and cpu time is
0.026976 seconds OR 26.976 milliseconds.
1) Are my assumptions correct?
2) 6.976 milliseconds is accounted for my the scheduler switch delay?
#include <stdio.h>
#include <sys/time.h>
#include <time.h>
#define MAX_PRIME 100000
void do_primes()
{
unsigned long i, num, primes = 0;
for (num = 1; num <= MAX_PRIME; ++num)
{
for (i = 2; (i <= num) && (num % i != 0); ++i);
if (i == num)
++primes;
}
printf("Calculated %ld primes.\n", primes);
}
int main()
{
struct timeval t1, t2;
double elapsedTime;
clock_t start, end;
double cpu_time_used;
int primes = 0;
int i = 0;
int num = 0;
start = clock();
/* start timer*/
gettimeofday(&t1, NULL);
/*do something */
usleep(20000);
do_primes();
/* stop timer*/
gettimeofday(&t2, NULL);
end = clock();
/*compute and print the elapsed time in millisec*/
elapsedTime = (t2.tv_sec - t1.tv_sec) * 1000.0; /* sec to ms*/
elapsedTime += (t2.tv_usec - t1.tv_usec) / 1000.0; /* us to ms */
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
printf("elapsed time %f sec. \ncpu time used %f secs.\n",(elapsedTime/1000),cpu_time_used);
return 0;
}
Your understanding is correct.
The additional 6.976ms might not mean anything at all, because it's possible that the clock() function only has a resolution of 10ms.

Resources