Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为CC_SHA256_更新优化了批量读取_C_Openssl_Sha256 - Fatal编程技术网

为CC_SHA256_更新优化了批量读取

为CC_SHA256_更新优化了批量读取,c,openssl,sha256,C,Openssl,Sha256,我有以下代码来计算输入的sha_256,我想知道如何知道CC_SHA256_更新每次迭代的最佳数据块大小。它是一个常量值还是一个依赖于系统环境的变量 CC_SHA256_CTX sha256; CC_SHA256_Init(&sha256); const long bufSize = 32768; //how can i find the optimized size ? char* buffer = (char *) malloc(bufSize); int bytes

我有以下代码来计算输入的sha_256,我想知道如何知道CC_SHA256_更新每次迭代的最佳数据块大小。它是一个常量值还是一个依赖于系统环境的变量

 CC_SHA256_CTX sha256;
 CC_SHA256_Init(&sha256);

 const long bufSize = 32768; //how can i find the optimized size ? 
 char* buffer = (char *) malloc(bufSize);

 int bytesRead = 0;
 if(!buffer) {
     return -1;
 }

 while((bytesRead = (int) fread(buffer, 1, bufSize, file))) {
     CC_SHA256_Update(&sha256, buffer, bytesRead);
 }
编辑:我尝试了下面所选答案中描述的不同方法,并使用mmap(而不是malloc+fread)获取数据。不幸的是,id并没有提高运行时效率(它略微提高了)


我认为只有使用不同大小的测试才能清楚地表明这一点,但64kB(分配粒度)的倍数可能是首选


<>但是,对于最佳性能,您可以考虑直接使用<代码>文件< /> >。这将消除将所有数据从内核模式(OS磁盘缓存)复制到用户模式的需要。您将直接访问操作系统缓存,并且可能只需要调用一次
CC\u SHA256\u Update()

底层文件系统缓存管理器可能会读取最佳大小的块,因此您不必太担心。基准测试可能是最好的工具。另请参见,,…
Plus One
,了解内存映射I/Ohi,不幸的是,mmap方法并没有提高代码的效率。也许您可以看看我上面的实现,如果您认为我可以改进它,请告诉我。thanks@Zohar81-根据SHA256所需的(处理器)时间,速度增益可能很小(还考虑到这是一次仅向前读取,操作系统已经对其进行了优化),但不应花费更长的时间。但是尝试使用
MAP\u PRIVATE
而不是
MAP\u SHARED
。另外,不要忘记调用
munmap()
(并检查
mmap()
的返回值)。
int fsize(const char *filename) {
    struct stat st; 
    if (stat(filename, &st) == 0)
        return st.st_size;
    return -1; 
}

int fd = open(path, O_RDONLY);

int sz = fsize(path);  
char * buffer = mmap((caddr_t)0, sz, PROT_READ  , MAP_SHARED, fd, 0);

CC_SHA256_CTX sha256;
CC_SHA256_Init(&sha256);

CC_SHA256_Update(&sha256, buffer, sz);

CC_SHA256_Final(output, &sha256);

close(fd);
return 0;