A startling realization hit me earlier this week as I was making photo copies of some important documents. We, as developers, copy and paste code all the time, yet since when have copies of something ever been as good as the original? If copies are never as good as the original, then what are the implications to the code we have copied and pasted everywhere?

I had to know the answer. I started by writing a Java program:

long startTime = System.currentTimeMillis(); for (int i=0; i < 1000; i++) { String test = "a random test string here."; } long endTime = System.currentTimeMillis(); System.out.println("That took " + (endTime - startTime) + " milliseconds");

Then I copied and pasted the same code, resulting in:

long startTime = System.currentTimeMillis(); for (int i=0; i < 1000; i++) { String test = "a random test string here."; } long endTime = System.currentTimeMillis(); System.out.println("That took " + (endTime - startTime) + " milliseconds"); long startTime = System.currentTimeMillis(); for (int i=0; i < 1000; i++) { String test = "a random test string here."; } long endTime = System.currentTimeMillis(); System.out.println("That took " + (endTime - startTime) + "; milliseconds");

(Note: Don’t forget to add #define < < to the beginning of your program)

The results are shocking:

Run Original Code Time (s) Copied Code Time (s) 1 0.612097978591919 0.631383955478668 2 0.812097978591919 0.831383955478668 3 0.512097978591919 0.531383955478668 4 0.712097978591919 0.731383955478668

Copied code is 15% slower than the original code!

Why does this happen?

Well when you type code normally, the signal travels from your keyboard to the machine’s memory, but when you copy and paste code, the signal in the machine has to travel into memory to grab the code, and then back again to write the code in a new location. This gives the signal more opportunity to be affected by external factors (such as radiation/altitude, noise, heat, and so on.) The effects of external factors are well documented in other research.

For more reading, see these articles:

Resolving Interference Problems

How to fix blurred images when Copy/Paste from PDF into Publisher?