Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Написать программу, реализующую обмен (чтение/запись) данных по схемам "Оперативная память - Внешняя память" или "Внешняя память - Внешняя память",
- * и продемонстрировать ее работу на тестовом примере. Данные для тестового примера генерируются случайным образом.
- * Вариант №5:
- * Дан текстовой файл размером не менее 100 Кбайт, содержащий строки различной длины.
- * Считать этот файл в оперативную память построчно. Операцию повторить 20 раз.
- * Характеристика: максимальный размер строк (диапазон изменения 64-128 байт).
- * от 1 до (64,75,95,128)
- */
- #include "stdio.h"
- #include "stdlib.h"
- #include "stdint.h"
- #include "windows.h"
- char generateRandomSymbol()
- {
- return ((char)((rand() % (126 + 1 - 33)) + 33));
- }
- void generateString(char* buff, int len)
- {
- for(int i = 0; i < len; i++)
- {
- *buff++ = generateRandomSymbol();
- }
- }
- int main()
- {
- const int FILE_MIN_LENGTH = 1024 * 100;
- const int NUMBER_OF_FILES = 4;
- const int MAX_STRING_SIZE = 256;
- const int NUMBER_OF_METERINGS = 20;
- const int MILLISECONDS_PER_SEC = 1000;
- const int FILE_MAX_LENGTH = FILE_MIN_LENGTH + MAX_STRING_SIZE;
- const int MAX_NUMBER_OF_STRINGS_IN_FILE = (FILE_MIN_LENGTH / 64);
- HANDLE files[NUMBER_OF_FILES];
- float fileReadTimings[NUMBER_OF_FILES];
- char* filenames[NUMBER_OF_FILES];
- filenames[0] = "file0.txt";
- filenames[1] = "file1.txt";
- filenames[2] = "file2.txt";
- filenames[3] = "file3.txt";
- int stringSizes[NUMBER_OF_FILES];
- stringSizes[0] = 64;
- stringSizes[1] = 75;
- stringSizes[2] = 95;
- stringSizes[3] = 128;
- int* endOfStringPositions[NUMBER_OF_FILES];
- for(int i = 0; i < NUMBER_OF_FILES; i++)
- {
- endOfStringPositions[i] = malloc( sizeof(int) * MAX_NUMBER_OF_STRINGS_IN_FILE);
- }
- for(int i = 0; i < NUMBER_OF_FILES; i++)
- {
- files[i] = CreateFile(filenames[i], GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
- }
- char buff[MAX_STRING_SIZE];
- char endOfString = '\n';
- int currentStringLength;
- int bytesWritten;
- for(int currentFile = 0; currentFile < NUMBER_OF_FILES; currentFile++)
- {
- for(int carriage = 0, currentString = 0; carriage < FILE_MIN_LENGTH; currentString++ )
- {
- currentStringLength = ((rand() % (stringSizes[currentFile])) + 1);
- generateString(buff, currentStringLength);
- WriteFile(files[currentFile], buff, currentStringLength, &bytesWritten, NULL);
- carriage += currentStringLength;
- WriteFile(files[currentFile], &endOfString, 1, &bytesWritten, NULL);
- carriage += 1;
- endOfStringPositions[currentFile][currentString] = carriage;
- }
- SetFilePointer(files[currentFile], 0, NULL, FILE_BEGIN);
- }
- uint64_t beginTicksTiming, endTicksTiming, ticksPerSecond;
- QueryPerformanceFrequency(&ticksPerSecond);
- char HugeBuffForFile[FILE_MAX_LENGTH];
- for(int currentFile = 0; currentFile < NUMBER_OF_FILES; currentFile++)
- {
- fileReadTimings[currentFile] = 0;
- for(int currentMetering = 0; currentMetering < NUMBER_OF_METERINGS; currentMetering++)
- {
- QueryPerformanceCounter(&beginTicksTiming);
- for(int carriage = 0, currentString = 0; ; currentString++ )
- {
- ReadFile(files[currentFile], &HugeBuffForFile[carriage], endOfStringPositions[currentFile][currentString], &bytesWritten, NULL);
- if( bytesWritten == 0 )
- {
- break;
- }
- carriage += endOfStringPositions[currentFile][currentString];
- }
- QueryPerformanceCounter(&endTicksTiming);
- fileReadTimings[currentFile] += (endTicksTiming - beginTicksTiming);
- SetFilePointer(files[currentFile], 0, NULL, FILE_BEGIN);
- }
- fileReadTimings[currentFile] /= ticksPerSecond;
- fileReadTimings[currentFile] *= MILLISECONDS_PER_SEC;
- }
- printf("%i meterings per file:\n", NUMBER_OF_METERINGS);
- printf("% 8s% 18s % 16s\n", "Filename", "Max. string size", "Time elapsed(ms)");
- for(int currentFile = 0; currentFile < NUMBER_OF_FILES; currentFile++)
- {
- printf("% 8s% 17i % 16f\n", filenames[currentFile], stringSizes[currentFile], fileReadTimings[currentFile]);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement