与 mmap (c)、fopen(c) 和 ifstream(c++) 的差异结果
Difference results with mmap (c), fopen(c) and 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;
}
mmap:
#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
循环,因为 ptr[i]
总是迭代 size
次可以合法地是任何字节值,并且它无法发出信号 EOF
.
我有 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;
}
mmap:
#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
循环,因为 ptr[i]
总是迭代 size
次可以合法地是任何字节值,并且它无法发出信号 EOF
.