Can a function return 2 values?
I have one or more functions separate from main() , that besides the return value , must also return the time it tooked to process.
How can I do that ?
Eko
Junior Poster in Training
60 posts since Nov 2006
Reputation Points: 12
Solved Threads: 1
make a parameter that is a pointer to the object you want the function to return Example:
int foo( size_t* time)
{
*time = 123;
return 1;
}
int main()
{
size_t processing_time;
int x = foo( &processing_time );
}
If you want to profile the entire function then the function doesn't need to know anything about it.
int main()
{
time_t start, end;
start = time(0);
foo();
end = time(0);
// now just subtract the two times to get processing time
return 0l;
}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
After you said , at the first method ,i came up with this:
#include <stdio.h>
#include <time.h>
int foo( clock_t* time)
{
int x=0;
while(x<1000000000)
x++;
*time=clock()/ (long) CLK_TCK;
return x;
}
int main()
{
clock_t processing_time;
int x = foo( &processing_time );
printf("%d\n x value is %d= ",processing_time,x);
return 0;
}
I don't know if it's the greatest solution , but it works(i quess),only that isn't very accuarate .How can i make it display small time values(like 0.001 seconds)
The second method doesn't work
Eko
Junior Poster in Training
60 posts since Nov 2006
Reputation Points: 12
Solved Threads: 1
see my second example -- you have to get the time before processing starts and again after, then subtract the two. And the time is in whole numbers. If you want fractions than divide by 100 when printing it.
print ("time = %f\n", (float)processing_time/100.0F);
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
Well for the second example , I get " error C2064: term does not evaluate to a function taking 1 arguments" at the start and end temp(0) :sad:
Eko
Junior Poster in Training
60 posts since Nov 2006
Reputation Points: 12
Solved Threads: 1
did you include time.h? Also, if you are compiling as c++ then change variable end to something else because end is a c++ key word.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
#include <stdio.h>
#include <time.h>
#include <dos.h>
int foo()
{
int x=0;
while(x<1000000)
x++;
return x;
}
int main()
{
time_t start, end;
int x,time;
start = time(0);
x=foo();
end = time(0);
// now just subtract the two times to get processing time
time=end-start;
printf("%d s: ",time);
return 0;
}
Eko
Junior Poster in Training
60 posts since Nov 2006
Reputation Points: 12
Solved Threads: 1
You were right.It was that time variable :cheesy:
Thanks Ancient Dragos,Ravalon
Eko
Junior Poster in Training
60 posts since Nov 2006
Reputation Points: 12
Solved Threads: 1