如何通过多次调用获得程序的性能?
How to get performance of a program over multiple invocations?
我正在尝试检查程序的性能。我有以下程序:
/* stdlibrary_writer.c ‐ write 400,000 characters with fprintf */
#include <stdio.h>
#define OUTPUTNAME "fprint.out"
main()
{
long i;
FILE *fp;
if ((fp=fopen(OUTPUTNAME,"w")) == NULL) {
fprintf(stderr,"Can't open %s. Bye.\n",OUTPUTNAME);
exit(1);
}
for (i=0; i<400000; i++) { /* write 400,000 Xs with fprintf */
if (fprintf(fp,"X") < 1) {
fprintf(stderr,"Can't write. Bye\n");
exit(1);
}
}
fclose(fp);
exit(0);
}
我想对这个程序进行 10 次连续调用的计时。我怎样才能做到这一点?
我想使用 Unix 的 /usr/bin/time
命令来为程序计时。
您可以编译程序,然后在执行它时使用时间来了解它的性能,如下所示,
$ gcc stdlibrary_writer.c -o stdlibrary_writer.o
$ time ./stdlibrary_writer.o
对于连续 10 次性能检查,您需要 运行 将其变成这样的循环,
c=1
while [ $c -le 10 ]
do
time ./stdlibrary_writer.o
(( c++ ))
done
如果您正在查看从 Unix shell 执行程序所消耗的时间,请使用 Linux 时间,如下所示,
time ./a.out
real 0m0.001s
user 0m0.000s
sys 0m0.000s
其次,如果你想花时间在程序代码 (C) 中执行语句数,请尝试使用 gettimeofday(),如下所示,
#include <sys/time.h>
struct timeval tv1, tv2;
gettimeofday(&tv1, NULL);
/* Program code to execute here */
gettimeofday(&tv2, NULL);
printf("Time taken in execution = %f seconds\n",
(double) (tv2.tv_usec - tv1.tv_usec) / 1000000 +
(double) (tv2.tv_sec - tv1.tv_sec));
当然你需要编译你的程序。对于基准测试,要求编译器 optimization 使用 -O2
,例如
gcc -Wall -O2 -g stdlibrary_writer.c -o stdlibrary_writer
您可以像
这样的 shell 循环计时
time for i in $(seq 1 10) ; do ./stdlibrary_writer ; done
但实际上您可能应该测量 10 次然后计算平均值(& 最小值和最大值),例如
for i in $(seq 1 10) ; do echo round $i; time ./stdlibrary_writer ; done
请注意,您可以通过将上述命令输送到某些 awk(1) 脚本(您应该自己编写)来自动计算平均值、最小值和最大值。
顺便说一句,在 GNU printf(3) 中使用 perror
,或 strerror(errno)
,或 %m
来处理错误,例如
if ((fp=fopen(OUTPUTNAME,"w")) == NULL) {
perror(OUTPUTNAME);
exit(EXIT_FAILURE);
}
或
if ((fp=fopen(OUTPUTNAME,"w")) == NULL) {
fprintf(stderr,"Can't open %s. (%s), Bye.\n",
OUTPUTNAME, strerror(errno));
exit(EXIT_FAILURE);
}
或
if ((fp=fopen(OUTPUTNAME,"w")) == NULL) {
fprintf(stderr,"Can't open %s. (%m), Bye.\n",
OUTPUTNAME);
exit(EXIT_FAILURE);
}
你需要阅读time(7)
我正在尝试检查程序的性能。我有以下程序:
/* stdlibrary_writer.c ‐ write 400,000 characters with fprintf */
#include <stdio.h>
#define OUTPUTNAME "fprint.out"
main()
{
long i;
FILE *fp;
if ((fp=fopen(OUTPUTNAME,"w")) == NULL) {
fprintf(stderr,"Can't open %s. Bye.\n",OUTPUTNAME);
exit(1);
}
for (i=0; i<400000; i++) { /* write 400,000 Xs with fprintf */
if (fprintf(fp,"X") < 1) {
fprintf(stderr,"Can't write. Bye\n");
exit(1);
}
}
fclose(fp);
exit(0);
}
我想对这个程序进行 10 次连续调用的计时。我怎样才能做到这一点?
我想使用 Unix 的 /usr/bin/time
命令来为程序计时。
您可以编译程序,然后在执行它时使用时间来了解它的性能,如下所示,
$ gcc stdlibrary_writer.c -o stdlibrary_writer.o
$ time ./stdlibrary_writer.o
对于连续 10 次性能检查,您需要 运行 将其变成这样的循环,
c=1
while [ $c -le 10 ]
do
time ./stdlibrary_writer.o
(( c++ ))
done
如果您正在查看从 Unix shell 执行程序所消耗的时间,请使用 Linux 时间,如下所示,
time ./a.out
real 0m0.001s
user 0m0.000s
sys 0m0.000s
其次,如果你想花时间在程序代码 (C) 中执行语句数,请尝试使用 gettimeofday(),如下所示,
#include <sys/time.h>
struct timeval tv1, tv2;
gettimeofday(&tv1, NULL);
/* Program code to execute here */
gettimeofday(&tv2, NULL);
printf("Time taken in execution = %f seconds\n",
(double) (tv2.tv_usec - tv1.tv_usec) / 1000000 +
(double) (tv2.tv_sec - tv1.tv_sec));
当然你需要编译你的程序。对于基准测试,要求编译器 optimization 使用 -O2
,例如
gcc -Wall -O2 -g stdlibrary_writer.c -o stdlibrary_writer
您可以像
这样的 shell 循环计时 time for i in $(seq 1 10) ; do ./stdlibrary_writer ; done
但实际上您可能应该测量 10 次然后计算平均值(& 最小值和最大值),例如
for i in $(seq 1 10) ; do echo round $i; time ./stdlibrary_writer ; done
请注意,您可以通过将上述命令输送到某些 awk(1) 脚本(您应该自己编写)来自动计算平均值、最小值和最大值。
顺便说一句,在 GNU printf(3) 中使用 perror
,或 strerror(errno)
,或 %m
来处理错误,例如
if ((fp=fopen(OUTPUTNAME,"w")) == NULL) {
perror(OUTPUTNAME);
exit(EXIT_FAILURE);
}
或
if ((fp=fopen(OUTPUTNAME,"w")) == NULL) {
fprintf(stderr,"Can't open %s. (%s), Bye.\n",
OUTPUTNAME, strerror(errno));
exit(EXIT_FAILURE);
}
或
if ((fp=fopen(OUTPUTNAME,"w")) == NULL) {
fprintf(stderr,"Can't open %s. (%m), Bye.\n",
OUTPUTNAME);
exit(EXIT_FAILURE);
}
你需要阅读time(7)