getrusage性能

#include <stdio.h>
#include <stdlib.h>

#include <sys/time.h>
#include <sys/resource.h>
 
/**
* test in 32-bit, Red Hat Enterprise Linux AS release 4
* Linux 2.6.9-55.ELsmp #1 SMP Fri Apr 20 17:03:35 EDT 2007 i686 i686 i386 GNU/Linux
* 1. donot use rusage, loop for 2200000000 times:
* real  0m9.903
* user  0m9.881s
* sys   0m0.004s
* 2. use rusage, loop for 2200000000 times:
* 3. donot use rusage, loop for 10000000 times:
* real  0m0.048s
* user  0m0.046s
* sys   0m0.002s
* 4. use rusage, loop for 10000000 times:
* real  0m6.654s
* user  0m2.251s
* sys   0m4.403s 
* 5. donot use rusage, loop for 100000000 times:
* real  0m0.456s
* user  0m0.451s
* sys   0m0.001s
* 6. use rusage, loop for 100000000 times:
* real  1m8.746s
* user  0m21.391s
* sys   0m47.186s
*/
int main(int argc, char** argv){
        for(int i = 0; i < 100000000; i++){
        }exit(0);
        for(;;){
                rusage r;
                if(getrusage(RUSAGE_SELF, &r) == -1){
                        printf("error!\n");
                        exit(1);
                }
                printf("utime=%d, stime=%d, maxrss=%d\n", r.ru_utime, r.ru_stime, r.ru_maxrss);
        }
        return 0;
}


也就是说,空载时系统的执行速度是:219.298次/微秒

执行getrusage函数的速度是:1.45463次/微秒。

你可能感兴趣的:(getrusage性能)