473,811 Members | 4,047 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

GCC time.h problem.

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.Anywa y 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?

Mar 3 '06 #1
25 17090
Jaideep wrote:
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?
Mar 3 '06 #2
Jaideep wrote:
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.Anywa y 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.

--
Eric Sosman
es*****@acm-dot-org.invalid
Mar 3 '06 #3

Jaideep wrote:
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.Anywa y 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.

Mar 3 '06 #4
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

--
Linux Registered User #334216
Get FireFox! >> http://www.spreadfirefox.com/?q=affiliates&id=48183&t=1
Staff >> http://www.debianizzati.org <<
Mar 3 '06 #5
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.Regard ing 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] = i;

start = clock();

i = 1;

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

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

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

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

r = n;
s = i+1;
while (r > s)
{
temp = pi[r];
pi[r] = pi[s];
pi[s] = 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();
}
}

Mar 3 '06 #6
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.

Mar 3 '06 #7

David Paleino wrote:
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).


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.

Mar 3 '06 #8
Jaideep wrote:
Hi!
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] = i;

start = clock();

i = 1;

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

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

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

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

r = n;
s = i+1;
while (r > s)
{
temp = pi[r];
pi[r] = pi[s];
pi[s] = 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.

Mar 3 '06 #9
Jaideep wrote:
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/

--
BR, Vladimir

It's not an optical illusion, it just looks like one.
-- Phil White

Mar 3 '06 #10

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

Similar topics

6
12956
by: David Graham | last post by:
Hi I have asked this question in alt.php as the time() function as used in setcookie belongs to php - or does it belong equally in the javascript camp - bit confused about that. Anyway, can anyone here put me straight on the following: I had a look at the time() function came across this: "To clarify, it seems this function returns the time of the computer's clock and does not do any timezone adjustments to return GMT, so you are...
10
4793
by: Bart Goeman | last post by:
Hi, I have a question about how to put redundant information in data structures, initialized at compile time. This is often necessary for performance reasons and can't be done at run time (data structures are read only) Ideally one should be able to put the redundant information there automatically so no mistakes are possible, but in a lot of case I see no way how to do it.
38
2567
by: vashwath | last post by:
Might be off topic but I don't know where to post this question.Hope some body clears my doubt. The coding standard of the project which I am working on say's not to use malloc.When I asked my lead(I have just started working) he said we should not use dynamic allocation in real time systems, the code will not run in predictable time period.Can anybody tell what does he mean?Why the execution time becomes unpredictable? Thanks
3
1485
by: Servé Lau | last post by:
I encountered a situation today where time(NULL) returned -1. This was because at the point of the call the function could not read from the bus because it was locked by another process. I got the advice to use while((t = time(NULL)) == -1) { ; } I always thought that time only returned -1 when their was no timer functionality in the hardware and that it should return a valid value for all other cases. Is that correct? Should the loop...
9
7291
by: HL | last post by:
I am using VS 2005 Beta - C# Problem: The Timer fires a few milliseconds before the actual Due-Time Let's say a timer is created in the following manner: System.Threading.Timer m_timer = null; Let's declare a constant Int32 m_TimePeriod = 10000;
7
3022
by: Shimon Sim | last post by:
I have a custom composite control I have following property
80
3483
by: Charles Law | last post by:
Hi guys I have a time critical process, running on a worker thread. By "time critical", I mean that certain parts of the process must be completed in a specific time frame. The time when the process starts is not especially important, but it must be complete within a small number of seconds. The operations I am performing do not take a long time (hundreds of milliseconds), but as each part of the process is complete, my worker thread...
3
2748
by: Satish Itty | last post by:
Hi all, I have a big problem in my hands and not sure how I can fix this. Any suggestions would be greatly appreciated. I have a .NET 3 tier app developed in VS2003 and .NET 1.1. the client is a windows application and middle tier runs on IIS. Data is passed between the client and middle tier as DataSets and custom value object classes. The problem in my hand is that the client application is deployed in different time zones and the...
5
4055
by: Omer | last post by:
hi Everyone, I am using ASP.Net 2.0. When user logins, I check the credential and then made the cookie. My hoster's server is in Arizona region and I am in Pakistan. I set cookie's expiration time as 4 hours. It works perfectly fine on my PC and many other PCs which have correct time. But, if I set date to some old date, user is simply unable to login. This makes sense as probably cookie timing is not matching. Dilemma is that many users...
4
2472
by: Ken Fine | last post by:
I've been living with a frustrating issue with VS.NET for some months now and I need to figure out what the problem is. Hopefully someone has run into the same issue and can suggest a fix. I suspect some permissions problem. I'm running VS.NET 2008 in Vista. Symptoms and observations: * ASP.NET's native ImageMap and Image controls work just fine and provide a design-time preview of images that are referenced via the ImageUrl property *...
0
9724
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
10379
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10127
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
9201
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...
0
6882
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
5552
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...
0
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4336
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
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.