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 ?

Recommended Answers

All 9 Replies

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;
}

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

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);

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:

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.

#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;
}
#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;
}

You've named one of your variables time, and that conflicts with the time() function.

You were right.It was that time variable :cheesy:

Thanks Ancient Dragos,Ravalon

#include<stdio.h>

int incre(int, int);
void main()
{
int a,b;
scanf("%d%d",&a,&b);
printf("incre a %d%d",incre(a,b),incre(b,a));
}
int incre(int x,int y)
{
int temp;
x=temp;
x=y;
y=x;
return x,y;
}

commented: Just another stupid poster, 3 years late with the "void main" program and no code tags - nothing to see here, move along. -4
commented: I can't imagine the confusion of ideas that gives rise to code like that. -4
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.