[Include] MapAndreas Include
#39

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?

Код:
    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);
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:
Код:
File took 50ms to open 1000 times and close it again
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.
Reply


Messages In This Thread
MapAndreas Include - by RyDeR` - 01.08.2011, 00:50
Re: MapAndreas Include - by iPLEOMAX - 01.08.2011, 00:54
Re: MapAndreas Include - by MrDeath537 - 01.08.2011, 01:00
Re: MapAndreas Include - by Donya - 01.08.2011, 01:06
Re: MapAndreas Include - by Toni - 01.08.2011, 01:27
Re: MapAndreas Include - by SlashPT - 01.08.2011, 01:30
Respuesta: MapAndreas Include - by D3IMON - 01.08.2011, 01:53
Re: MapAndreas Include - by [LoD]Hauke - 01.08.2011, 02:06
Re: MapAndreas Include - by RyDeR` - 01.08.2011, 08:07
Re: MapAndreas Include - by wups - 01.08.2011, 09:01
Re: MapAndreas Include - by QuaTTrO - 01.08.2011, 09:16
Re: MapAndreas Include - by RyDeR` - 01.08.2011, 09:28
Re: MapAndreas Include - by HyperZ - 01.08.2011, 09:34
Re: MapAndreas Include - by vyper - 01.08.2011, 10:55
Re: MapAndreas Include - by Zh3r0 - 01.08.2011, 11:28
Re: MapAndreas Include - by Lorenc_ - 01.08.2011, 11:53
Re: MapAndreas Include - by Hiddos - 01.08.2011, 12:10
Re: MapAndreas Include - by Lorenc_ - 01.08.2011, 12:13
Re: MapAndreas Include - by CoaPsyFactor - 01.08.2011, 12:31
Re: MapAndreas Include - by RyDeR` - 01.08.2011, 12:40
Re: MapAndreas Include - by JackBauer. - 01.08.2011, 13:24
Re: MapAndreas Include - by RyDeR` - 01.08.2011, 13:34
Re: MapAndreas Include - by TheArcher - 01.08.2011, 14:03
AW: MapAndreas Include - by Drebin - 01.08.2011, 15:07
Re: MapAndreas Include - by RyDeR` - 01.08.2011, 22:06
Re: MapAndreas Include - by Lorenc_ - 02.08.2011, 09:36
Re: MapAndreas Include - by RyDeR` - 02.08.2011, 09:55
Re: MapAndreas Include - by Lorenc_ - 02.08.2011, 10:07
Re: MapAndreas Include - by TheArcher - 02.08.2011, 10:35
Re : Re: MapAndreas Include - by Naruto_Emilio - 20.08.2011, 01:49
AW: MapAndreas Include - by -Viper- - 31.03.2012, 21:11
Re: MapAndreas Include - by Hiddos - 06.04.2012, 09:20
Re: MapAndreas Include - by RyDeR` - 06.04.2012, 10:58
Re: MapAndreas Include - by SublimESmokeR420 - 12.04.2012, 00:56
Re: MapAndreas Include - by SublimESmokeR420 - 14.04.2012, 02:41
Re: MapAndreas Include - by RyDeR` - 21.04.2012, 10:21
Re: MapAndreas Include - by xMaGmOtAnEtHx - 18.08.2012, 01:17
Re: MapAndreas Include - by FUNExtreme - 18.08.2012, 01:30
Re: MapAndreas Include - by PowerPC603 - 29.12.2013, 23:00
Re: MapAndreas Include - by Alphlax - 29.12.2013, 23:23

Forum Jump:


Users browsing this thread: 5 Guest(s)