954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

a little prob with TIME

im tryin to compare the time it takes to calculate squareroot by the stdlib function and recursive function. everythin's workin good except the TIME thing. its givein me the same time. wat is wrong??? plz help me find it.. thank you guys!

#include
#include
#include
#include
#include

using namespace std;

double sqroot(double,double,double);

void main()
{
int j,k,dif; double x,y,z; double a,b;
z=0.01; y=2;

cout<<"Enter the number you want to find the squareroot of : ";
cin>>x;

a=sqrt(x);
j=time(0);

b=sqroot(x,y,z);
k=time(0);

dif=k-j;

cout<<"By using the cstdlib the ans is :"<

ohnbabygal
Newbie Poster
12 posts since Sep 2004
Reputation Points: 11
Solved Threads: 0
 

I've never seen the time function but I would just guess it would have to look like this.

a=sqrt(x);
j=time(sqrt(x));

jasweb2002
Junior Poster in Training
56 posts since Sep 2004
Reputation Points: 11
Solved Threads: 2
 

Declare time variables like such

clock_t start, end;

start=clock();
end=clock();

cout<<"the total elapsed time is: "<<(end-start)/CLK_TCK<<"seconds"<

Jpowers22
Newbie Poster
5 posts since Oct 2004
Reputation Points: 12
Solved Threads: 0
 

Jpowers22 is right, you need to use clock() to get to something that is about a millisecond. You also have to do the calculation about a million times to get a meaningful time to measure. Yes Ruby, computers are fast nowadays!
[php]// time the sqrt() function Dev-C++

#include
#include
#include

using namespace std;

int main()
{
int k;
double a,x,y,z;
clock_t start, end;

z = 0.01;
y = 2;
cout << "Enter the number you want to find the squareroot of : ";
cin >> x;

start = clock();
for(k = 0; k < 1000000; k++)
a = sqrt(x);
end = clock();
cout << "1M iterations of sqrt() from cmath took ";
cout << (end - start) << " ticks\n"; // a tick is about 1 ms

// now do a similar thing for sqroot(x,y,z)

system("PAUSE");
return 0;
}
[/php]

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You