与 mmap (c)、fopen(c) 和 ifstream(c++) 的差异结果

问题描述

我有 3 个不同的程序(mmap、fopen、ifstream)来计算文件中某个字符的出现次数。我这样做是为了测试从内存中读取文件的不同技术的性能。但是即使我得到了相同的 ifstream 和 fopen 计数,mmap 的计数也高于其他两个,我无法检测到原因。

ifstream:

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <time.h>

using namespace std;

int main(){

printf("\n");

clock_t begin = clock();

ifstream file;
string filename = "loremipsum.txt";
file.open(filename.c_str());


char ch;
int count = 0;

while(file.get(ch)){

    if(ch == 'a'){
        count++;
    }
}

clock_t end = clock();



double time_spent = 0.0;
time_spent += (double)(end - begin) / CLOCKS_PER_SEC;

cout << "Number of 'a' in file: " << count << endl;
cout << "Time elapsed for counting: " << time_spent << endl;

printf("\n");

return 0;
}

打开:

#include <stdio.h>
#include <stdlib.h>
#include <time.h> 
#include <sys/stat.h>
#include <fcntl.h>


int main(void)
{
printf("\n");

FILE *fp;
int id;
char ch;
double time_spent = 0.0;
int count = 0;

clock_t begin = clock();
if ((fp = fopen ("loremipsum.txt","r")) == NULL) {
    printf("Dosya açma hatası!");
    exit(1);
}

ch = fgetc ( fp ) ;



while(ch != EOF){

  
    if(ch == 'a'){
        count += 1;
    }

    ch = fgetc ( fp ) ;
}

clock_t end = clock();






time_spent += (double)(end - begin) / CLOCKS_PER_SEC;

printf("Time elapsed is %f seconds\n",time_spent);

printf("number of character 'a' is %d\n",count);
fclose(fp);

printf("\n");
return 0;
}

映射:

#include <iostream>
#include <stdio.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h> 
#include <unistd.h>


int main(void){


int fd = open ("loremipsum.txt",O_RDONLY);
struct stat s;
size_t size;
int status = fstat(fd,&s);
size = s.st_size;
double time_spent = 0.0;

clock_t begin = clock();

char *ptr = (char*) mmap(0,size,PROT_READ,MAP_PRIVATE,fd,0);

if(ptr == MAP_FAILED){

    printf("Mapping failed\n");
    return 1;
}



int i = 0,count = 0;
while(ptr[i] != EOF){
    if(ptr[i] == 'a'){
        count++;
    }
    i++;
}

clock_t end = clock();


printf("\nnumber of a is %d\n",count);

time_spent += (double)(end - begin) / CLOCKS_PER_SEC;

printf("Time elapsed is %f seconds\n",time_spent);


status = munmap(ptr,size);

if(status != 0){
    
    printf("Unmapping failed\n");
    return 1;
}
close(fd);

printf("\n");
return 0;


}

解决方法

在 mmap 版本中,ptr[i] != EOF 检查看起来非常可疑,应该将循环更改为始终精确迭代 for 次的 size 循环,因为 ptr[i] 可以合法地是任何字节值,并且它无法发出 EOF 信号。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...