Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <assert.h>
- #include <stdio.h>
- #include <string.h>
- #include <fcntl.h>
- #include <sys/uio.h>
- #include <time.h>
- #include <stdlib.h>
- #include <unistd.h>
- #define _COUNT_BUFS 8
- #define _BUF_LEN 64
- time_t test_std_read() {
- int fd = open("book.txt", O_RDONLY);
- char buf[_BUF_LEN] ;
- ssize_t nr;
- time_t start_std = clock();
- while ( (nr=read(fd, buf, _BUF_LEN))!=0)
- write(STDOUT_FILENO, buf, _BUF_LEN);
- write(STDOUT_FILENO, buf, _BUF_LEN);
- time_t time_std = clock() - start_std;
- close(fd);
- return time_std;
- }
- time_t test_v_read() {
- char memory_seg[_COUNT_BUFS][_BUF_LEN];
- struct iovec segments[_COUNT_BUFS];
- int fd = open("book.txt", O_RDONLY);
- assert(fd!=-1);
- ssize_t nr;
- time_t start = clock();
- for(int i=0; i<_COUNT_BUFS; i++) {
- segments[i].iov_base = memory_seg[i];
- segments[i].iov_len = sizeof(memory_seg[i]);
- }
- while ( (nr = readv(fd, segments, _COUNT_BUFS))!=0)
- writev(STDOUT_FILENO, segments, _COUNT_BUFS);
- writev(STDOUT_FILENO, segments, _COUNT_BUFS);
- time_t time_v = clock() - start;
- close(fd);
- return time_v;
- }
- int main() {
- time_t t1 = test_std_read();
- system("clear");
- time_t t2 = test_v_read();
- system("clear");
- printf("time to standart read = %ld\n", t1);
- printf("time to readv = %ld\n", t2);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment