Write a program that will measure the average access time to internal and external memory (reading data from main memory and external memory, which is diskette 3.5). Compare and justify the results. In the absence of a floppy drive is taken flash drive is taken. And there, and there is the same file.

FILE *f; long c; c = clock(); f = fopen("/home/Документы/file.txt", "r"); for (; fgetc(f) != EOF;) ; c = (clock() - c) / 1000; printf("hardw read______\n%ldms\n", c); printf("%fmb/s\n", 262143.0 / (float)c); // мегабайт в сек close(f); c = clock(); f = fopen("/media/flash/file.txt", "r"); for (; fgetc(f) != EOF;) ; c = (clock() - c) / 1000; printf("flash read______\n%ldms\n", c); printf("%fmb/s\n", 262143.0 / (float)c); close(f); 

As a result, I get a very similar speed. Is everything right? Or am I somewhere not there or not measuring? Thank.

  • file somewhere on 1mb make - Alex Kapustin
  • and what will happen if you try to read something big (hundreds of megabytes)? - beardog
  • shurik, that is 256 megabytes a lot? - studentus pm
  • No, I thought that the file is small. + maybe it has been cached since you often read it. Try on a new test to do a new file. Better yet, write the file on one computer and read on another, although remounting should reset the cache. The code seems to be right - Alex Kapustin

2 answers 2

In a similar task, the file should be read in large blocks (say 64Kbyte). It would be more correct to use open () / read () and not fopen () / fread (), especially not fgetc ().

Otherwise, you basically measure memory / memory overhead.

In general, here (speed with files) you need to clearly understand what speed you want to measure - with or without the file cache (that is, when someone first reads the file in the system). If you measure with a cache, then the file must be read several times. Without taking into account the cache - take a really large file (say gigabytes).

To measure the speed of memory, select 2 large (hundreds of megabytes, say 1/2 RAM) blocks and copy calling memcpy (). For small blocks, you will measure the performance of the processor cache. Measure in a cycle, do not take into account the first measurement.

  • avp, something I did not quite understand how to do it. can you explain? It is desirable with the code, not quite how to allocate memory. (Last paragraph) - studentus
  • Allocate memory - malloc (). - avp
  • thank you) passed before your answer) - studentus

I did not understand. The task is to compare the speed of reading from RAM and from disk. And you compare the reading from the hard drive, where your home directory is, with a flash drive. It seems to me that something is not going on here.

In general, the speed of reading from modern flash drives is quite high. So if the file is small, then more time will be spent on the system call, internal disassembly, access to the I / O port, waiting for a response, etc., than on the actual reading.

  • I myself suspect that I am doing something wrong in the first case. therefore asked a question. Do not tell me how to work with RAM directly? - studentus
  • Well, they already wrote - memcpy () - skegg