-
#include <sys/mman.h>
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <fcntl.h>
-
#include <string.h>
-
#include <unistd.h>
-
#include <string.h>
-
-
-
#define TIMER(val) do{
-
struct timeval tm;
-
gettimeofday(&tm, NULL);
-
val = tm.tv_sec * 1000 + tm.tv_usec/1000;
-
}
-
while(0)
-
-
int i = 0;
-
-
int create_tmp_file()
-
{
-
char dir[100] = "/tmp/mmap";
-
char file[100] = {0};
-
snprintf(file, 100, "%s/%d", dir, i);
-
i++;
-
-
int fd = open(file, O_RDWR | O_CREAT);
-
if(fd<0)
-
{
-
printf("errorn");
-
exit(1);
-
}
-
return fd;
-
}
-
-
int create_tmp_file2()
-
{
-
char dir[100] = "/tmp/mmap";
-
char file[100] = {0};
-
snprintf(file, 100, "%s/%d", dir, i);
-
i++;
-
-
int fd = open(file, O_CREAT | O_RDWR | O_APPEND);
-
if(fd<0)
-
{
-
printf("errorn");
-
exit(1);
-
}
-
return fd;
-
}
-
-
char *mmap_fd(int fd, int size)
-
{
-
if(lseek(fd, size-1, SEEK_SET)==-1)
-
printf("lseek errorn");
-
if(write(fd, "0", 1)!=1)
-
printf("write errorn");
-
//if(fsync(fd)==-1)
-
// printf("fsync errorn");
-
char *p = (char *) mmap(NULL, size, PROT_WRITE, MAP_SHARED, fd, 0);
-
if(p==MAP_FAILED)
-
{
-
perror("mmap");
-
exit(1);
-
}
-
return p;
-
}
-
-
int main()
-
{
-
long starttime, endtime;
-
starttime = endtime = 0;
-
-
int size = 1024 * 5;
-
int count = 1000;
-
char test_string[1024] = "lkjklsdjffffffffffffffffffffffffffffaelkjklgjiowejklgjkljaskldj";
-
int test_string_len = strlen(test_string);
-
-
//1. test mmap
-
TIMER(starttime);
-
int fd = create_tmp_file();
-
char *p = mmap_fd(fd, size);
-
char *tmp_p = p;
-
-
int j =0;
-
int size_sum = 0;
-
-
for(; j<count; j++)
-
{
-
size_sum += test_string_len;
-
if(size_sum > size)
-
{
-
close(fd);
-
munmap(p, size);
-
fd = create_tmp_file();
-
p = mmap_fd(fd, size);
-
size_sum = test_string_len;
-
tmp_p = p;
-
}
-
-
memcpy(tmp_p, test_string, test_string_len);
-
tmp_p += test_string_len;
-
}
-
close(fd);
-
munmap(p, size);
-
TIMER(endtime);
-
printf("mmap duration: %ldn", endtime - starttime);
-
-
//2. write
-
size_sum = 0;
-
TIMER(starttime);
-
fd = create_tmp_file2();
-
for(j=0; j<count; j++)
-
{
-
size_sum += test_string_len;
-
if(size_sum > size)
-
{
-
close(fd);
-
fd = create_tmp_file2();
-
size_sum = test_string_len;
-
}
-
write(fd, test_string, test_string_len);
-
}
-
close(fd);
-
TIMER(endtime);
-
printf("batch write duration: %ldn", endtime - starttime);
-
-
//3. write
-
TIMER(starttime);
-
for(j=0; j<count; j++)
-
{
-
fd = create_tmp_file();
-
write(fd, test_string, test_string_len);
-
close(fd);
-
}
-
TIMER(endtime);
-
printf("loop write duration: %ldn", endtime - starttime);
-
-
//4. buffer write
-
size_sum = 0;
-
TIMER(starttime);
-
int index = 0;
-
char tmp_buffer[size];
-
fd = create_tmp_file2();
-
for(j=0; j<count; j++)
-
{
-
size_sum += test_string_len;
-
if(size_sum > size)
-
{
-
write(fd, tmp_buffer, size_sum-test_string_len);
-
close(fd);
-
fd = create_tmp_file2();
-
size_sum = test_string_len;
-
}
-
-
strncpy(tmp_buffer+size_sum-test_string_len, test_string, test_string_len);
-
}
-
write(fd, tmp_buffer, size_sum-test_string_len);
-
close(fd);
-
TIMER(endtime);
-
printf("buffer write duration: %ldn", endtime - starttime);
-
-
return 0;
-
}
gcc mmap-test.c -o mmaptest
./mmaptest
result:
mmap duration: 3
batch write duration: 8
loop write duration: 77
buffer write duration: 3
阅读(448) | 评论(0) | 转发(0) |