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

can't get the right time

the follow code is to test how long to run the loop

i wanna print the time as millisecond format

but the result of console is always 0.000

anyone can help me?

#include <cstdlib>
#include <iostream>
#include <time.h>

using namespace std;
int main(int argc, char *argv[])
{

int a[500][500];
int xx=10;
int i = 0 , j = 0;
for(i=0;i<500;i++)
for(j=0;j<500;j++)
a[i][j]=i;

clock_t startTime =clock();
for(i=0;i<500;i++)
for(j=0;j<500;j++)
xx=a[i][j];

cout<<"total time is:"<<( endTime - startTime ) /
CLOCKS_PER_SEC<<endl;
clock_t endTime = clock();

system("PAUSE");
return EXIT_SUCCESS;
}

May 10 '06 #1
3 1643
Jade King wrote:
cout<<"total time is:"<<( endTime - startTime ) /
CLOCKS_PER_SEC<<endl;


Next time don't cram everything on one line.

double total = ( endTime - startTime ) / CLOCKS_PER_SEC;
cout << "total time is:" << total << endl;

Now what's happening is slightly more aparent. Both (endTime - startTime)
and CLOCKS_PER_SEC are integral, not doubles, so / resolves to integral
division. Zero.

Make one a double to get double division:

double total = double( endTime - startTime ) / CLOCKS_PER_SEC;

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
May 10 '06 #2
Jade King wrote:
the follow code is to test how long to run the loop

i wanna print the time as millisecond format

but the result of console is always 0.000 cout<<"total time is:"<<( endTime - startTime ) /
CLOCKS_PER_SEC<<endl;
clock_t endTime = clock();

This compiles???

If so, where does endTime come from in the first line?

--
Ian Collins.
May 10 '06 #3
sorry

i copy it by mistake

May 10 '06 #4

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

Similar topics

6
by: lostinspace | last post by:
After four+ years of using FrontPage in a limited capacity and having created over 600 pages on my sites, I've finally (at least for the most part) abandoned FP, to begin the process of converting...
42
by: Mike P. | last post by:
Hello I come from the world of C++ programming, and I'm used to writing programs that are actually executed by the CPU, and that run with some semblance of performance. I have taken the time to...
119
by: rhat | last post by:
I heard that beta 2 now makes ASP.NET xhtml compliant. Can anyone shed some light on what this will change and it will break stuff as converting HTML to XHTML pages DO break things. see,...
6
by: Michel | last post by:
Can I invoke the context menu from javascript so I can have it disabled on 1 right-click, but show it on a double right-click or vice-versa?
9
by: Durgesh Sharma | last post by:
Hi All, Pleas help me .I am a starter as far as C Language is concerned . How can i Right Trim all the white spaces of a very long (2000 chars) Charecter string ( from the Right Side ) ? or how...
7
by: Earl | last post by:
Any known fixes for the wacky right-alignment bug in the WinForms datagrid (VS2003)? I've tried Ken's workaround...
7
by: Novice Computer User | last post by:
Hi. Can somebody PLEASE help. I have spent hours on this.. but I am a total novice and can't seem to figure it out. Here is a .php script. Right now, the minimum amount of time (i.e. duration)...
6
by: TulasiKumar | last post by:
hi all, i have one requirement in my project.The requirement is i want to fix some domain sites in TcpIp like proxy servers behaviour.When ever end user passing the inforamtion of that domain...
11
pbmods
by: pbmods | last post by:
A somewhat obscure hack has emerged recently that is an offshoot of the now-infamous XSS. It is known as Cross-Site Request Forgery, or XSRF for short. XSRF is a form of temporary identity theft...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.