Examining Cat Implementation Speeds

Summary

Your best bet is probably just to use whatever is packaged with your distro/os. The only real egregious contender was plan9port cat, having the least features, most assembly output, and slowest performance using the recommended compiler options. If you absolutely must know, out of the cat versions I tested, OpenBSD was the fastest.

Sidenote: This is not a thorough test, I was just bored and messing around, please don't treat this as a conclusive "study."

The Setup

The Hardware

inamiyar@inam-desktop
--------------------
OS: openSUSE Tumbleweed x86_64
Kernel: 5.6.11-1-default
Uptime: 2 hours, 45 mins
Packages: 2507 (rpm)
Shell: fish 3.1.2
Resolution: 1600x900, 1920x1080
WM: i3
Theme: Adwaita [GTK2/3]
Icons: Adwaita [GTK2/3]
Terminal: st
Terminal Font: Pragmata Pro
CPU: AMD Ryzen 5 2600 (12) @ 3.500GHz
GPU: AMD ATI Radeon RX 470/480/570/570X/580/580X/590
Memory: 3642MiB / 7875MiB

The Software

Everything compiled with "install" instructions. OpenBSD was a port I found on github but I confirmed the cat code was exactly the same as the upstream OpenBSD repository.

The Procedure

I ran four tests on each version of cat, and I ran each of those tests four times and averaged them.

Results

Lines of Code Disassembly Size Blank Time 999 Char Time 999k Char Time 999k Char Concat Time
GNU 708 225KiB 0.00157s 0.002s 0.07725s 0.13275s
Plan9 36 350KiB 0.0023925s 0.01875s 0.091s 0.16025s
OpenBSD 215 81KiB 0.0017875s 0.01175s 0.08125 0.13825

Conclusion

OpenBSD is the fastest in almost everything that matters. Something a little strange I found was that trying to run cat on a file with OpenBSD added a lot of overhead for small files (the 999 character test) which was...strange? I looked at the code for an obvious reason for this but didn't find anything too interesting. Either way we're talking about 1-2 milliseconds of savings, and the BSD cat is measurably faster in every other test.

I don't know if it's not an optimized compiled or what, but for some reason the dead-simple 36-line implementation of cat from plan9 is the slowest by far, has the largest disassembly footprint, and doesn't even support the one argument needed to be POSIX compliant (-u). On the other hand, the GNU cat I compiled was slightly slower than the BSD cat, but has significantly more features, and despite having a more complicated code-base is still very pleasant to read because of how well-commented it is. On top of that, for some reason the GNU cat that was shipped with OpenSUSE performs significantly faster than the ones I compiled (and around as fast as the OpenBSD tools), I didn't really research why this would be and omitted it from the results.