Guest User

Untitled

a guest
Jul 11th, 2019
555
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. #include <assert.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <fcntl.h>
  5. #include <sys/uio.h>
  6. #include <time.h>
  7. #include <stdlib.h>
  8. #include <unistd.h>
  9.  
  10. #define _COUNT_BUFS 8
  11. #define _BUF_LEN 64
  12.  
  13. time_t test_std_read() {
  14. int fd = open("book.txt", O_RDONLY);
  15. char buf[_BUF_LEN] ;
  16. ssize_t nr;
  17. time_t start_std = clock();
  18. while ( (nr=read(fd, buf, _BUF_LEN))!=0)
  19. write(STDOUT_FILENO, buf, _BUF_LEN);
  20. write(STDOUT_FILENO, buf, _BUF_LEN);
  21. time_t time_std = clock() - start_std;
  22. close(fd);
  23. return time_std;
  24. }
  25. time_t test_v_read() {
  26. char memory_seg[_COUNT_BUFS][_BUF_LEN];
  27. struct iovec segments[_COUNT_BUFS];
  28. int fd = open("book.txt", O_RDONLY);
  29. assert(fd!=-1);
  30. ssize_t nr;
  31. time_t start = clock();
  32. for(int i=0; i<_COUNT_BUFS; i++) {
  33. segments[i].iov_base = memory_seg[i];
  34. segments[i].iov_len = sizeof(memory_seg[i]);
  35. }
  36. while ( (nr = readv(fd, segments, _COUNT_BUFS))!=0)
  37. writev(STDOUT_FILENO, segments, _COUNT_BUFS);
  38. writev(STDOUT_FILENO, segments, _COUNT_BUFS);
  39. time_t time_v = clock() - start;
  40. close(fd);
  41. return time_v;
  42. }
  43. int main() {
  44. time_t t1 = test_std_read();
  45. system("clear");
  46. time_t t2 = test_v_read();
  47. system("clear");
  48. printf("time to standart read = %ld\n", t1);
  49. printf("time to readv = %ld\n", t2);
  50.  
  51. return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment