While writing scripts we often have to save data in temporary files for further processing. As we know the workhorse programs of GNU/Linux (like grep, sed, awk, cut, head, sort, uniq …) work well with files and not with variables. Writing to hard disk is SLOW, yes SLOW. Even on SSD ones. Well things tend to get better on SSD, but still. I need to test them myself.
What is FAST and efficient for temporary files is RAM. Writing and reading temporary files from RAM is awesome. But how do we do it? Easy of course! Mount a directory, which is physically located in RAM with file system type tmpfs or ramfs.
# mkdir /mnt/temp
// if you want user access
# chown -R user:user /mnt/temp/
// fixed size, uses swap, vollatile, all in ram
# mount -t tmpfs -o size=20m,uid=1000,gid=1000 tmpfs /mnt/temp/
or
// dynamic size, no swap, vollatile, all in ram
# mount -t ramfs -o size=20m,uid=1000,gid=1000 tmpfs /mnt/temp/
Now you can read/write as much as you want, as fast as you can! Let us test the theory. We can create a single 1 Giga Byte file with writing 1000 times one Mega Byte chunks each. To create a 1GB file in RAM it takes about one second.
# time dd if=/dev/zero of=/mnt/tmpfs/1GB_file.txt bs=1M count=1000
1000+0 records in
1000+0 records out
1048576000 bytes (1.0 GB) copied, 1.18641 s, 884 MB/s
real 0m1.189s
user 0m0.000s
sys 0m1.056s
# time dd if=/dev/zero of=/mnt/tmpfs/1GB_file.txt bs=1M count=1000
1000+0 records in
1000+0 records out
1048576000 bytes (1.0 GB) copied, 1.06656 s, 983 MB/s
real 0m1.198s
user 0m0.000s
sys 0m1.180s
# time dd if=/dev/zero of=/mnt/tmpfs/1GB_file.txt bs=1M count=1000
1000+0 records in
1000+0 records out
1048576000 bytes (1.0 GB) copied, 1.02866 s, 1.0 GB/s
real 0m1.147s
user 0m0.004s
sys 0m1.128s
So do you still wonder how Google returns millions of links in a split of a second? I guess they keep the whole Internet (or the processed database out of it) in their RAM.
Now writing to the hard drive. I repeat the test several times to show the cache effect and that the tests are consistent. So to create 1GB file on the hard disk it takes about 25 seconds.
# time dd if=/dev/zero of=/mnt/sda2/vboxshare/1GB_file.txt bs=1M count=1000
1000+0 records in
1000+0 records out
1048576000 bytes (1.0 GB) copied, 30.82 s, 34.0 MB/s
real 0m30.871s
user 0m0.000s
sys 0m1.936s
# time dd if=/dev/zero of=/mnt/sda2/vboxshare/1GB_file.txt bs=1M count=1000
1000+0 records in
1000+0 records out
1048576000 bytes (1.0 GB) copied, 22.7691 s, 46.1 MB/s
real 0m22.993s
user 0m0.000s
sys 0m2.152s
# time dd if=/dev/zero of=/mnt/sda2/vboxshare/1GB_file.txt bs=1M count=1000
1000+0 records in
1000+0 records out
1048576000 bytes (1.0 GB) copied, 19.4128 s, 54.0 MB/s
real 0m25.087s
user 0m0.004s
sys 0m2.292s
# time dd if=/dev/zero of=/mnt/sda2/vboxshare/1GB_file.txt bs=1M count=1000
1000+0 records in
1000+0 records out
1048576000 bytes (1.0 GB) copied, 20.7324 s, 50.6 MB/s
real 0m23.648s
user 0m0.000s
sys 0m2.276s
# time dd if=/dev/zero of=/mnt/sda2/vboxshare/1GB_file.txt bs=1M count=1000
1000+0 records in
1000+0 records out
1048576000 bytes (1.0 GB) copied, 20.0088 s, 52.4 MB/s
real 0m23.806s
user 0m0.004s
sys 0m2.124s

Well there you have it – about 20 times faster on my mashine. My laptop hard drive is not very fast, but that is the case with my RAM too.