Advertisement
Guest User

Untitled

a guest
Jan 24th, 2018
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.91 KB | None | 0 0
  1. /**
  2. * Написать программу, реализующую обмен (чтение/запись) данных по схемам "Оперативная память - Внешняя память" или "Внешняя память - Внешняя память",
  3. * и продемонстрировать ее работу на тестовом примере. Данные для тестового примера генерируются случайным образом.
  4. * Вариант №5:
  5. * Дан текстовой файл размером не менее 100 Кбайт, содержащий строки различной длины.
  6. * Считать этот файл в оперативную память построчно. Операцию повторить 20 раз.
  7. * Характеристика: максимальный размер строк (диапазон изменения 64-128 байт).
  8. * от 1 до (64,75,95,128)
  9. */
  10.  
  11. #include "stdio.h"
  12. #include "stdlib.h"
  13. #include "stdint.h"
  14. #include "windows.h"
  15.  
  16. char generateRandomSymbol()
  17. {
  18. return ((char)((rand() % (126 + 1 - 33)) + 33));
  19. }
  20.  
  21. void generateString(char* buff, int len)
  22. {
  23. for(int i = 0; i < len; i++)
  24. {
  25. *buff++ = generateRandomSymbol();
  26. }
  27. }
  28.  
  29. int main()
  30. {
  31. const int FILE_MIN_LENGTH = 1024 * 100;
  32. const int NUMBER_OF_FILES = 4;
  33. const int MAX_STRING_SIZE = 128;
  34. const int NUMBER_OF_METERINGS = 200;
  35. const int MILLISECONDS_PER_SEC = 1000;
  36. const int FILE_MAX_LENGTH = FILE_MIN_LENGTH + MAX_STRING_SIZE;
  37. const int MAX_NUMBER_OF_STRINGS_IN_FILE = (FILE_MIN_LENGTH / 64);
  38.  
  39. HANDLE files[NUMBER_OF_FILES];
  40. float fileReadRates[NUMBER_OF_FILES];
  41.  
  42. char filenames[NUMBER_OF_FILES];
  43. filenames[0] = "file0.txt";
  44. filenames[1] = "file1.txt";
  45. filenames[2] = "file2.txt";
  46. filenames[3] = "file3.txt";
  47.  
  48. int stringSizes[NUMBER_OF_FILES];
  49. stringSizes[0] = 64;
  50. stringSizes[1] = 75;
  51. stringSizes[2] = 95;
  52. stringSizes[3] = 128;
  53.  
  54. int* endOfStringPositions[NUMBER_OF_FILES];
  55. for(int i = 0; i < NUMBER_OF_FILES; i++)
  56. {
  57. endOfStringPositions[i] = malloc( sizeof(int) * MAX_NUMBER_OF_STRINGS_IN_FILE);
  58. }
  59.  
  60. for(int i = 0; i < NUMBER_OF_FILES; i++)
  61. {
  62. files[i] = CreateFile(filenames[i], GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  63. }
  64.  
  65. char buff[MAX_STRING_SIZE];
  66. char endOfString = '\n';
  67. int currentStringLength;
  68. int bytesWritten;
  69. for(int currentFile = 0; currentFile < NUMBER_OF_FILES; currentFile++)
  70. {
  71. for(int carriage = 0, currentString = 0; carriage < FILE_MIN_LENGTH; currentString++ )
  72. {
  73. currentStringLength = ((rand() % (stringSizes[currentFile])) + 1);
  74. generateString(buff, currentStringLength);
  75.  
  76. WriteFile(files[currentFile], buff, currentStringLength, &bytesWritten, NULL);
  77. carriage += currentStringLength;
  78.  
  79. WriteFile(files[currentFile], &endOfString, 1, &bytesWritten, NULL);
  80. carriage += 1;
  81.  
  82. endOfStringPositions[currentFile][currentString] = carriage;
  83. }
  84. SetFilePointer(files[currentFile], 0, NULL, FILE_BEGIN);
  85. }
  86.  
  87. uint64_t beginTicksTiming, endTicksTiming, ticksPerSecond;
  88. QueryPerformanceFrequency(&ticksPerSecond);
  89. char* HugeBuffForFile = malloc(FILE_MAX_LENGTH * sizeof(char));
  90. for(int currentFile = 0; currentFile < NUMBER_OF_FILES; currentFile++)
  91. {
  92. fileReadRates[currentFile] = 0;
  93. for(int currentMetering = 0; currentMetering < NUMBER_OF_METERINGS; currentMetering++)
  94. {
  95. QueryPerformanceCounter(&beginTicksTiming);
  96. for(int carriage = 0, currentString = 0; ; currentString++ )
  97. {
  98. ReadFile(files[currentFile], &HugeBuffForFile[carriage], endOfStringPositions[currentFile][currentString], &bytesWritten, NULL);
  99. if( bytesWritten == 0 )
  100. {
  101. break;
  102. }
  103. carriage += endOfStringPositions[currentFile][currentString];
  104. }
  105. QueryPerformanceCounter(&endTicksTiming);
  106. fileReadRates[currentFile] += (endTicksTiming - beginTicksTiming);
  107.  
  108. SetFilePointer(files[currentFile], 0, NULL, FILE_BEGIN);
  109. }
  110. fileReadRates[currentFile] /= NUMBER_OF_METERINGS;
  111. fileReadRates[currentFile] /= ticksPerSecond;
  112. fileReadRates[currentFile] = FILE_MIN_LENGTH / (1024 * 1024 * fileReadRates[currentFile]);
  113. }
  114.  
  115. printf("% 8s% 18s % 16s\n", "Filename", "Max. string size", "Read speed(kbps)");
  116. printf("===========================================\n");
  117. for(int currentFile = 0; currentFile < NUMBER_OF_FILES; currentFile++)
  118. {
  119. printf("% 8s% 17i % 16.2f\n", filenames[currentFile], stringSizes[currentFile], fileReadRates[currentFile]);
  120. }
  121.  
  122. return 0;
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement