c 计时

#include <stdio.h>
#include <sys/time.h>
#include <math.h>

int f(int x){
	int s=0;
	while(x-- >0) s+=f(x);
	return fmax(s,1);
}

int main(int argc, char const *argv[])
{
	struct timeval start, end;
	gettimeofday( &start, NULL );
	f(35);
	gettimeofday( &end, NULL );
	int timeuse = 1000000 * ( end.tv_sec - start.tv_sec ) + end.tv_usec - start.tv_usec;
	printf("time: %d us\n", timeuse);
	return 0;
}

the result is:

clang: warning: treating 'c' input as 'c++' when in C++ mode, this behavior is deprecated
time: 273646936 us
[Finished in 273.7s]

你可能感兴趣的:(c 计时)