linux - zLib透明写入模式 "wT"性能下降

标签 linux performance io zlib

我希望 zLib 透明模式 ( gzptintf() ) 与常规 fprintf() 一样快。我发现带有“wT”的 zLib gzprintf() 比 fprintf() 慢 2.5 倍。有没有解决此性能问题的方法?

详细信息:

我在 Linux(fedora 22、kernel 4.0.5、Intel(R) Core(TM) i7-3770 CPU @ 3.40GHz)上使用 libz.so.1.2.8 为我的事件提供输出文件压缩选项痕迹收集器。为了保持旧版兼容性,我需要透明文件格式写入模式。

正如我所见,gzopen 中的选项“T”允许写入不压缩且没有 gzip header 记录的文件。

问题出在性能上。透明模式比简单标准 fprintf 慢约 2.5 倍。

这是快速测试结果(值以 TSC 为单位):

    zLib]$ ./zlib_transparent
Performance fprintf vs gzprintf (transparent):
     fprintf 22883026324
zLib transp  62305122876
ratio 2.72277

此测试的来源:

#include <stdio.h>
#include <zlib.h>
#include <iostream>
#include <sstream>
#include <iomanip>

#define NUMITERATIONS 10000000
static double buffer[NUMITERATIONS];

static __inline__ unsigned long long rdtsc(void){
    unsigned hi, lo;
    __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
    return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 );
}

long long test_fprintf(double *buffer){
    long long t = rdtsc();
#ifdef USE_FPRINTF
    double tmp = 0;
    FILE *file = fopen("fprintf_file.txt", "w");
    for (int i = 0; i < NUMITERATIONS; ++i) {
        fprintf(file, "[%f:%f]\n", buffer[i], buffer[i] - tmp);
        tmp = buffer[i] + i;
    }
    fclose(file);
#endif
    return rdtsc() - t;
}

long long test_zlib_transparent(double *buffer){
    long long t = rdtsc();
#ifdef USE_ZLIB
    double tmp = 0;
    gzFile file = gzopen("zlib_file.txt.gz", "wT");
    for (int i = 0; i < NUMITERATIONS; ++i) {
        gzprintf(file, "[%f:%f]\n", buffer[i], buffer[i] - tmp);
        tmp = buffer[i] + i;
    }
    gzclose(file);
#endif
    return rdtsc() - t;
}


int main(){
    std::cout << "Performance fprintf vs gzprintf (transparent):" << std::endl;
    long long dPrint = test_fprintf(buffer);
    std::cout << "     fprintf " << dPrint << std::endl;

    long long dStream = test_zlib_transparent(buffer);
    std::cout << "zLib transp  " << dStream << std::endl;

    std::cout << "ratio " << double(dStream)/double(dPrint) << std::endl;
    return 0;
}

构建:

g++ -g -O3 -DUSE_ZLIB=1 -DUSE_FPRINTF=1 zlib_transparent.cpp -o zlib_transparent –lz

谢谢

谢尔盖

最佳答案

我的错。 (我写了gzprintf()。)

write() 调用过于频繁。如果将 fprintf() 替换为 snprintf()write(),您将获得与 zlib 大致相同的性能。

我将在下一版本的 zlib 中改进这一点。如果您想尝试一下,请申请this diff 。我不知道它在 Linux 上的表现如何,但在 Mac OS X 上,透明模式下的 gzprintf() 现在比 fprintf() 快 10% 。 (没想到会这样。)

关于linux - zLib透明写入模式 "wT"性能下降,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31220932/

相关文章:

php - simpleSAMLphp SP 元数据返回 http 的 entityID 而不是预期的 https

r - 对矩阵索引求和时避免嵌套 for 循环

java - 对于 Android 事件,为什么 switch 语句比 if-else 链更常见?

java - 终止服务时关闭 ScheduledExecutorService

linux - 如何从bz2文件中提取数据而不解压文件

C++:std::async 和 std::mutex 在 Linux 上导致死锁但在 Windows 上运行?

performance - Postgres tsearch 在第一个查询上的性能

ruby - 为什么 IO::WaitReadable 对于 STDOUT 的引发方式与 STDERR 的引发方式不同?

io - 在 Julia 中设置多个 I/O 缓冲区的最佳方法是什么?

c - 你如何确定C中文件的大小?