
Temporal locality enhances performance. It tells us whether memory locations in a program are likely to be accessed again in the near future. For example, a method has high temporal locality if it is called repeatedly in a short period of time. With this principle in mind, we can develop effective optimizations for many programs.
This article describes temporal locality. It provides optimization tips.
One way to optimize a program is to increase its temporal locality. Let's think of a program that reads in 1000 files, generates a report for each file, and then writes 1000 output files. You could write this program by reading in one file, generating a report, and then writing an output file; this would be repeated 1000 times.
Program outline with low temporal locality Read data file Generate output Write output file Read data file Generate output Write output file Read data file Generate output Write output file Program outline with higher temporal locality Read data file Read data file Read data file Generate output Generate output Generate output Write output file Write output file Write output file

Increase temporal locality. So how can you change the program as described above to have more temporal locality? One thing you could try would be to read in all the data files at once; then generate all 1000 outputs; and finally write all 1000 output files.
Why would increasing temporal locality in the manner described in the example improve performance? First, in the example file IO is probably a large part of the total time required. If you read or write 1000 files in a tight loop, the underlying file system cache will anticipate this better. This will lead to less wait time when requesting a file be read in from the disk.

Additionally, by increasing temporal locality in this way, the code itself will be more likely to be in the cache. So it will execute faster as well. Basically, improving temporality locality here can greatly increase the effectiveness of caches on the hardware.
Results. Unfortunately, a realistic example of using temporal locality in this way is hard to demonstrate in a short article. However, my experience with a program that reads and writes several thousand (2000+) files was that improving temporal locality for the writing of the output files improved performance more than expected.

Compiler theory describes all storage locations as memory, including processor caches, RAM, and hard disks. Computers have a distinct memory hierarchy; with temporal locality, your program can be executed faster in this hierarchy because caches can actually anticipate your program's next actions better.
File Handling