29.12.2013, 23:00
(
Последний раз редактировалось PowerPC603; 30.12.2013 в 09:59.
)
I guess Y-Less isn't correct.
If he would be correct (that opening a file reads the entire file into memory), opening a file of 1.7 Gbytes 1000 times and closing it again would take at least an hour, depending on harddrive speed, would it not?
I put this code inside OnFilterscriptInit and put a file in my scriptfiles folder which is 1.7Gbytes in size.
It's a movie (.mkv file), just renamed to something easier (test.ini), the kind of data in the file doesn't matter as we won't be reading data from it anyway.
I ran this and the result inside my server console was:
Wow, I must have a very fast harddrive.
Loading a movie-file of 1.7Gbytes for 1000 times.
That's 1.7 Tbytes of data in 50ms.
That would mean my harddrive goes as fast as 34 Terabytes per second reading speed.
Wrong.
Opening a file only lets the OS reserve that file for your program and it returns a filehandle (stored in FileFile in my example) to your program, like a memory-address. After opening the file, the address points to the first byte in the file.
FSeek just sets an offset to that address.
And you can read the data at that location instantly and close the file afterwards.
Why is it slow?
A harddrive cannot read a single byte of data.
It reads an entire sector which is several kilobytes in size and only returns what was asked from that data.
That's the reason why it's slow.
You could read 16Kbytes of data from that sector, even when you only need 1 byte from it.
It's like opening a book.
When someone tells you to open a book (fopen), do you read the entire book instantly after opening it?
Nope.
After opening the book, someone tells you to go to a certain location in the book (fseek).
You do that by going to the given page.
If someone then asks you to read character 900 from that page, can you instantly point to it?
No, you need to read the entire page to find it (read an entire sector).
When you found it, close the book again (fclose).
Did you need to read the entire book to find one value? Nope.
If he would be correct (that opening a file reads the entire file into memory), opening a file of 1.7 Gbytes 1000 times and closing it again would take at least an hour, depending on harddrive speed, would it not?
Код:
new File:DFile, Count; Count = GetTickCount(); for (new i; i < 1000; i++) { if (fexist("test.ini")) { // Open the FileRadios for reading DFile = fopen("test.ini", io_read); // Close the file fclose(DFile); } } printf("File took %i ms to open 1000 times and close it again", GetTickCount() - Count);
It's a movie (.mkv file), just renamed to something easier (test.ini), the kind of data in the file doesn't matter as we won't be reading data from it anyway.
I ran this and the result inside my server console was:
Код:
File took 50ms to open 1000 times and close it again
Loading a movie-file of 1.7Gbytes for 1000 times.
That's 1.7 Tbytes of data in 50ms.
That would mean my harddrive goes as fast as 34 Terabytes per second reading speed.
Wrong.
Opening a file only lets the OS reserve that file for your program and it returns a filehandle (stored in FileFile in my example) to your program, like a memory-address. After opening the file, the address points to the first byte in the file.
FSeek just sets an offset to that address.
And you can read the data at that location instantly and close the file afterwards.
Why is it slow?
A harddrive cannot read a single byte of data.
It reads an entire sector which is several kilobytes in size and only returns what was asked from that data.
That's the reason why it's slow.
You could read 16Kbytes of data from that sector, even when you only need 1 byte from it.
It's like opening a book.
When someone tells you to open a book (fopen), do you read the entire book instantly after opening it?
Nope.
After opening the book, someone tells you to go to a certain location in the book (fseek).
You do that by going to the given page.
If someone then asks you to read character 900 from that page, can you instantly point to it?
No, you need to read the entire page to find it (read an entire sector).
When you found it, close the book again (fclose).
Did you need to read the entire book to find one value? Nope.