GCC time.h problem.

J

Jaideep

Hello!
I am new to GCC.I've abt 4-5 yrs of experience in windows
'C' programming.I find GCC much slower than turbo or microsoft
compilers.Anyway I wrote this small piece of code to calculate process
time .The code works fine in windows compilers but gives erratic output
in GCC.What could be the problem?

code-->
....
#include <time.h>
.....
void main() {

clock_t start, end;

start = clock();
//process();

end = clock();
printf("The time was: %f\n", (end - start) / CLK_TCK);
}


Is there any other way of doing the same?
 
T

Tim Prince

Jaideep said:
Anyway I wrote this small piece of code to calculate process
time .The code works fine in windows compilers but gives erratic output
in GCC.What could be the problem?

code-->
...
#include <time.h>
....
void main() {

clock_t start, end;

start = clock();
//process();

end = clock();
printf("The time was: %f\n", (end - start) / CLK_TCK);
}


Is there any other way of doing the same?
c.l.c deals with standard C. Did you try a standard C version of this?
e.g. replace CLK_TCK by (double)CLOCKS_PER_SEC
What makes you so sure that all "windows compilers" will work the same
with non-standard code?
 
E

Eric Sosman

Jaideep said:
Hello!
I am new to GCC.I've abt 4-5 yrs of experience in windows
'C' programming.I find GCC much slower than turbo or microsoft
compilers.Anyway I wrote this small piece of code to calculate process
time .The code works fine in windows compilers but gives erratic output
in GCC.What could be the problem?

code-->
...
#include <time.h>
....
void main() {

clock_t start, end;

start = clock();
//process();

end = clock();
printf("The time was: %f\n", (end - start) / CLK_TCK);
}


Is there any other way of doing the same?

Possible reasons for "erratic output" (you ought to
have explained what you meant by that):

- You called the printf() function but failed to
#include <stdio.h>. Your program therefore has
undefined behavior.

- You declared the main() function as `void' instead
of `int'. Your program therefore has undefined
behavior.

- You used "%f" to print the result, but "%f" requires
a corresponding `double' value. You do not know the
type of the expression `(end - start) / CLK_TICK'
because each implementation gets to make its own
choice, so on an implementation where this expression
produces something other than a `double' your program
exhibits undefined behavior.

- On an implementation where `clock_t' and `CLK_TICK'
happen to be integers, you will compute a zero duration
for any interval of less than one second.

If you'll pardon my saying so, your "abt 4-5 yrs experience"
doesn't seem to have taught you much about C. Perhaps you've
been using some kind of C-ish dialect rather than C itself,
and have picked up a lot of misinformation and bad habits.
 
V

Vladimir S. Oka

Jaideep said:
Hello!
I am new to GCC.I've abt 4-5 yrs of experience in windows
'C' programming.I find GCC much slower than turbo or microsoft

I love the quotes in 'C'. LoL
compilers.Anyway I wrote this small piece of code to calculate process
time .The code works fine in windows compilers but gives erratic output
in GCC.What could be the problem?

code-->

Not really, as it won't compile cut'n'pasted...
...
#include <time.h>
....
void main() {

This is your problem. After doing this, it's no longer C, and you may
observe weird stuff. Function `main` has to return an `int`.
clock_t start, end;

start = clock();
//process();

end = clock();
printf("The time was: %f\n", (end - start) / CLK_TCK);

Standard C does not mention CLK_TCK. Also, you don't include stdio.h,
so `printf` is unknown. And then, `clock_t` is integer type, and %f is
hardly the correct way of printing it.
 
D

David Paleino

Vladimir S. Oka ha scritto:
...

... Also, you don't include stdio.h,
so `printf` is unknown. ...

Well, just a point of note. We can't say "you don't include stdio.h", as
he makes a code ellipsis (see those dots around the `#include <time.h>'
line), so we don't actually know what he is including or not, or what
code is there between that inclusion and the `void main()' (which is
incorrect, as you already said).

David
 
J

Jaideep

Hi!
Of course I included stdio.h and other libraries.The code given is
not full.I've just included lines which I think might be
erronous.Regarding int main if I remove this clock thing program just
works fine.Also tried converting everything to float but still it
gives arbritrary values.Here is my full program which calculates no
series like 2 3 4 2 1 3 1 4 (2 nos between two 2's 3 betweeen two 3's
etc.

/*Number series calculation */

#include <stdio.h>
#include <stdlib.h>

#include <time.h>
#define n 8
char pi[n+1];

void check (void);

int main( void )
{
int r, s, temp;
register int i,j;
clock_t start, end;
float p;

for (i=0; i <= n; i++) pi = i;

start = clock();

i = 1;

while (i)
{
//for (i=1; i <= n; i++) printf( " %2d", pi );
//printf( "\n" );
check();

i = n-1;
while (pi > pi[i+1]) i--;

j = n;
while (pi > pi[j]) j--;

temp = pi;
pi = pi[j];
pi[j] = temp;

r = n;
s = i+1;
while (r > s)
{
temp = pi[r];
pi[r] = pi;
pi = temp;
r--; s++;
}
}
end = clock();
p=(double) (end - start) / CLK_TCK;

printf("The time was: %f\n", p);
//getch();
return 0;
}

void check()
{
int i[3*n],l,q=0,s=0;
register int j;
for(j=0;j<(3*n);j++) i[j]=0;
for(j=0;j<(2*n);j++)
{
++q;
while(i[j]!=0) j++;
l=pi[q];
i[j]=l;
if(i[(j+1+l)] != 0) break;
i[(j+1+l)]=l;
if((j+1+l) >((2*n)-1)) break;
s=s+1;
}
if(s==n)
{
for(j=0;j<2*n;j++) printf("%2d ", i[j]);
printf("\n");
//getch();
}
}
 
J

Jaideep

Hi!
Sorry I used int main(void) in program but wrote void main()
above.Program when run prints fixed process time no matter whatever
time it has taken.
 
V

Vladimir S. Oka

David said:
Vladimir S. Oka ha scritto:

Well, just a point of note. We can't say "you don't include stdio.h", as
he makes a code ellipsis (see those dots around the `#include <time.h>'
line), so we don't actually know what he is including or not, or what
code is there between that inclusion and the `void main()' (which is
incorrect, as you already said).

True, but since he went to the bother of including <time.h>, presumably
to justify using `clock()`, he should have done the same for
`printf()`. In any case, a minimal compilable example is what's needed,
especially in OPs.
 
V

Vladimir S. Oka

Jaideep said:

Please quote what and who you're replying to (and don't top-post while
you're at it). See http://cfaj.freeshell.org/google/ for advice.
Of course I included stdio.h and other libraries.The code given is
not full.I've just included lines which I think might be
erronous.

You should have tried first and made sure that the snippet you posted is
erroneous. Also, if it's not compilable, many people here won't bother
look, as they expect to cut'n'paste and see for themselves.
Regarding int main if I remove this clock thing program just
works fine.Also tried converting everything to float but still it
gives arbritrary values.Here is my full program which calculates no
series like 2 3 4 2 1 3 1 4 (2 nos between two 2's 3 betweeen two 3's
etc.

I haven't looked whether code below really does what you claim it does,
but now it does compile cleanly. It also gives time of 100 consistently
on my SUSE Linux 10.0 box, so I don't know what you mean by "erratic"
in your OP.

[code not snipped for the benefit of whoever wants a look]
/*Number series calculation */

#include <stdio.h>
#include <stdlib.h>

#include <time.h>
#define n 8
char pi[n+1];

void check (void);

int main( void )
{
int r, s, temp;
register int i,j;
clock_t start, end;
float p;

for (i=0; i <= n; i++) pi = i;

start = clock();

i = 1;

while (i)
{
//for (i=1; i <= n; i++) printf( " %2d", pi );
//printf( "\n" );
check();

i = n-1;
while (pi > pi[i+1]) i--;

j = n;
while (pi > pi[j]) j--;

temp = pi;
pi = pi[j];
pi[j] = temp;

r = n;
s = i+1;
while (r > s)
{
temp = pi[r];
pi[r] = pi;
pi = temp;
r--; s++;
}
}
end = clock();
p=(double) (end - start) / CLK_TCK;

printf("The time was: %f\n", p);
//getch();
return 0;
}

void check()
{
int i[3*n],l,q=0,s=0;
register int j;
for(j=0;j<(3*n);j++) i[j]=0;
for(j=0;j<(2*n);j++)
{
++q;
while(i[j]!=0) j++;
l=pi[q];
i[j]=l;
if(i[(j+1+l)] != 0) break;
i[(j+1+l)]=l;
if((j+1+l) >((2*n)-1)) break;
s=s+1;
}
if(s==n)
{
for(j=0;j<2*n;j++) printf("%2d ", i[j]);
printf("\n");
//getch();
}
}


--
BR, Vladimir

For perfect happiness, remember two things:
(1) Be content with what you've got.
(2) Be sure you've got plenty.
 
V

Vladimir S. Oka

Jaideep said:
Hi!
Sorry I used int main(void) in program but wrote void main()
above.Program when run prints fixed process time no matter whatever
time it has taken.

Even worse. it means you didn't even cut'n'paste into the post, but
re-typed. How then will we know whether your problem is in re-typing or
original code?

Again, quote, and generally follow the advice here:

http://cfaj.freeshell.org/google/
 
J

Jaideep

Thanks I'll follow the advice.In my case also It gives 100
everytime.But when I compile it in borland compiler it gives the exact
process time.What could be the problem?
 
V

Vladimir S. Oka

Jaideep said:
Thanks I'll follow the advice.

When? You still didn't quote.
In my case also It gives 100
everytime.But when I compile it in borland compiler it gives the exact
process time.What could be the problem?

If you happen to know the "exact process time", as you say, then you
don't need to measure in your code. Also, Linux and whatever M$ OS is
Borland running on, are hardly similar enough to give you same
execution times. Even Windoze implementation of gcc will have different
execution speeds.


--
BR, Vladimir

An aphorism is never exactly true;
it is either a half-truth or one-and-a-half truths.
-- Karl Kraus
 
K

Keith Thompson

Vladimir S. Oka said:
I love the quotes in 'C'. LoL


Not really, as it won't compile cut'n'pasted...


This is your problem. After doing this, it's no longer C, and you may
observe weird stuff. Function `main` has to return an `int`.

This is certainly *a* problem, but it's unlikely to be the cause of
whatever symptoms he's seeing. Declaring main to return void does
cause undefined behavior (unless the implementation happens to support
it as a documented extension), but the most common symptom of the
undefined behavior is that the program behaves as expected (and
possibly returns a garbage status to the calling environment). Fix
it, but don't stop looking for other errors.
Standard C does not mention CLK_TCK. Also, you don't include stdio.h,
so `printf` is unknown. And then, `clock_t` is integer type, and %f is
hardly the correct way of printing it.

clock_t is an arithmetic type capable of representing times; it may or
may not be an integer type.
 
V

Vladimir S. Oka

Keith said:
This is certainly *a* problem, but it's unlikely to be the cause of
whatever symptoms he's seeing. Declaring main to return void does
cause undefined behavior (unless the implementation happens to support
it as a documented extension), but the most common symptom of the
undefined behavior is that the program behaves as expected (and
possibly returns a garbage status to the calling environment). Fix
it, but don't stop looking for other errors.


clock_t is an arithmetic type capable of representing times; it may or
may not be an integer type.

Yes, I realised that just after posting by looking at the Standard -- I
know I should have looked first, but... I actually went and tried OP's
code, and compiler said something along the lines of you've told printf
it's float but passed an integer (can't retry now, as I'm on a
different tool chain).

--
BR, Vladimir

Duty, n:
What one expects from others.
-- Oscar Wilde
 
J

Jaideep

Vladimir said:
I haven't looked whether code below really does what you claim it does,
but now it does compile cleanly. It also gives time of 100 consistently
on my SUSE Linux 10.0 box, so I don't know what you mean by "erratic"
in your OP.

When I change value of n to 7,8,9 etc (I.e. 14,16,18 digit no o/p in my
code) execution time changes and hence the printed time output.But in
GCC it remains same no matter what the value of n is.The number series
O/Ps are perfect, Problem is in calculating execution time.Is there any
problem with my gcc configuration or program?
 
V

Vladimir S. Oka

Jaideep said:
When I change value of n to 7,8,9 etc (I.e. 14,16,18 digit no o/p in
my code) execution time changes and hence the printed time output.But
in GCC it remains same no matter what the value of n is.The number
series O/Ps are perfect, Problem is in calculating execution time.Is
there any problem with my gcc configuration or program?

I think you're placing too much trust in clock(). The number of ticks
per second (CLOCKS_PER_SEC, not CLK_TCK, is defined in the Standard) is
implementation defined and may not be good enough for your purposes. I
suggest you try execution profilers instead (e.g. gprof).
 
T

Tim Prince

Vladimir said:
Jaideep wrote:




I think you're placing too much trust in clock(). The number of ticks
per second (CLOCKS_PER_SEC, not CLK_TCK, is defined in the Standard) is
implementation defined and may not be good enough for your purposes. I
suggest you try execution profilers instead (e.g. gprof).
O/P has consistently ignored advice given in this thread. If he refuses
to fix outright errors in the code, there is little point in profiling.
The limitations of clock() have been discussed on c.l.f many times,
well beyond the FAQ information, which by itself would have saved a lot
of trashing in this thread. If what you say about CLOCKS_PER_SEC were
true, it would imply on most systems a much better resolution than is
available in gprof. gprof, how it can be used on Windows, and how is
broken (in patchable ways) on Red Hat x86-64, and most other 2.6.x
kernel implementations, goes well Off Topic.
 
J

Jaideep

I think you're placing too much trust in clock(). The number of ticks
per second (CLOCKS_PER_SEC, not CLK_TCK, is defined in the Standard) is
implementation defined and may not be good enough for your purposes. I
suggest you try execution profilers instead (e.g. gprof).

I remember using execution profiler in keil cross compiler.Can I
implement it in a standalone program?I want to create a benchmarking
module.I've used clock() function several times with borland
compilers.Also execution profiler assumes full processor usage and
doesn't consider other processes running in background(Which are
irrelavant to C program).
 
J

Jordan Abel

I think you're placing too much trust in clock(). The number of ticks
per second (CLOCKS_PER_SEC, not CLK_TCK, is defined in the Standard) is
implementation defined and may not be good enough for your purposes. I
suggest you try execution profilers instead (e.g. gprof).

Data point: CLOCKS_PER_SEC is one million on SUSv2 compliant unix
systems, but this is one point where some BSD systems don't conform to
that standard. For example, on FreeBSD, it is 128, and on OpenBSD, and
from what I can tell earlier versions of NetBSD, it is 100.

This implies nothing about the resolution of the clock, but on modern
systems it's more likely to be better than worse.
 
K

Keith Thompson

Jaideep said:
I remember using execution profiler in keil cross compiler.Can I
implement it in a standalone program?
[...]

We don't know. It's a question about whatever tool set you're using,
not a question about the language, so we can't help you with it here.

The line above:


is an attribution lines. Please don't snip those when you post a
followup. It's very helpful to know who said what. On the other
hand, pleaes *do* snip signatures unless you're actually commenting on
them.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,949
Members
47,500
Latest member
ArianneJsb

Latest Threads

Top