• Forum
  • Lounge
  • How does reading/writing in blocks work?

 
How does reading/writing in blocks work?

I have long been under the impression that looping over all values of a 32-bit integer will take a very long time no matter what operation you do on each iteration. However it occurred to me that we deal with large files (e.g. more than 2 or 4 GB) every day, and I realized it must be possible because we read and write in large chunks. But what is really going on to speed up this process? Is hardware just really that efficient and only software is slow?

Video processing also blows my mind.
Last edited on
I have long been under the impression that looping over all values of a 32-bit integer will take a very long time no matter what operation you do on each iteration.


It does. Assuming you can do 1 iteration in 1 cycle (read: you can't, it takes much longer)... you have 4 billion iterations... which means 4 billion cycles.

If you have a 4GHz processor, it would take 1 full second of using 100% cpu time. (EDIT: to clarify, 100% of a single thread)

But of course, like I say, any operation is going to take longer than 1 cycle... so it'll actually be much longer than that. If an iteration takes 10 cycles (more reasonable, but still probably shorter than it would actually be), that's 10 full seconds.

And that's assuming the bottleneck comes from the CPU (which it probably wouldn't)

So yeah. processing 4 GB of data is going to take a while.

However it occurred to me that we deal with large files (e.g. more than 2 or 4 GB) every day, and I realized it must be possible because we read and write in large chunks.


This all depends on the program you're using and how the data is being handled.

Case in point.. try to open a 4 GB text file in Notepad.
Then try to open the same file in Notepad++.

Notepad will take several (10-15?) seconds to load.
Notepad++ will load it virtually instantly.

It's not that Notepad++ has some super secret way of reading the data faster. It's that it doesn't try to read all the data at once. It only reads what it needs to in order to display to the user what they want to see.

Whereas Notepad will load the entire file into memory (slow) before showing anything to the user.


Smarter programs can do tricks like that to do the heavy processing 'on demand' or in the background so that the program stays responsive and fast.


Video processing also blows my mind.


That's another topic entirely ;P
Last edited on
Haha, for some reason even 1 hour is "fast" in my mind for iterating over all values of a 32-bit integer. I guess I got the misconception originally from using a software creation tool where loops do a lot more internal stuff. Setting the number of times to loop to -1 was considered infinite, but internally it just used a signed 32-bit integer and so theoretically could eventually overflow and reach -1 and end, but since there was so much other stuff going on to make the software work it was not reasonable to expect to see that even after days.

Thanks Disch!
Haha, for some reason even 1 hour is "fast" in my mind for iterating over all values of a 32-bit integer. I guess I got the misconception originally from using a software creation tool where loops do a lot more internal stuff.


Hah.

Yeah.... computers are FAST. Like... it's mind blowing how fast they are if you really stop to think about it.

Even something seemingly simple like moving your mouse around on the screen actually takes a bunch of work:

- OS communicates with HW in some manner to determine the mouse has moved.
- OS figures out which window(s) the mouse is over, sends the processes which own those windows a mouse move message
- Those programs forward that message to any child windows.
- All those windows do their own message handling, which may involve changing the cursor, redrawing portions of the window, etc
- Mouse may move off one window and into another, so the OS has to check for that and alert windows appropriate.
- OS has to redraw the cursor at its new position.


I mean it's a trivial operation, but when you look at the steps involved... the things that are happening are in the hundreds. And that's a single move... when you physically move the mouse it's usually interpreted as a dozen or so moves!

And all that work doesn't even register as a load on the machine. It breezes through it like it's nothing.


In some of the games and stuff I've written... I was taken aback at just how much work needs to be done every frame in order to get the game to work how I wanted it. Or to emulate a system like the NES/SNES. Yet modern computers handle it with ease.



It really is incredible. Modern computers are amazing.
I had never thought of that, wow. I do still remember on old Windows 95/98 machines at my elementary school that moving the mouse while the computer was logging you in used to actually cause noticeable slowdown and would noticeably take longer. I never noticed how much things improved since then.
And because every situation has an xkcd comic related http://xkcd.com/676/
You know what really blows my mind? Downloading 200mb file and getting agitated if it starts taking more than a couple of minutes.
Topic archived. No new replies allowed.