【发布时间】:2014-08-23 01:20:31
【问题描述】:
以下程序说明了这个问题:
生成文件:
CFLAGS = -O3 -std=c++0x
LDFLAGS = -lreadline
test: test.o
g++ $(CFLAGS) $< $(LDFLAGS) -o $@
test.o: test.cpp Makefile
g++ $(CFLAGS) -c $<
test.cpp:
#include <fnmatch.h>
#include <readline/readline.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
static double time()
{
timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
return ts.tv_sec + (1e-9 * (double)ts.tv_nsec);
}
static void time_fnmatch()
{
for (int i = 0; i < 2; i++)
{
double t = time();
for (int i = 0; i < 1000000; i++)
{
fnmatch("*.o", "testfile", FNM_PERIOD);
}
fprintf(stderr, "%f\n", time()-t);
}
}
int main()
{
time_fnmatch();
char *input = readline("> ");
free(input);
time_fnmatch();
}
输出:
0.045371
0.044537
>
0.185246
0.181607
在调用 readline() 之前,fnmatch 调用大约快 4 倍。虽然 这种性能差异令人担忧,我最感兴趣的是找出 readline() 调用究竟对程序状态做了什么 会对其他库调用产生这种影响。
【问题讨论】: