Τρίτη, Ιουνίου 25, 2013

Text substitution racing

I was bored. Don't judge me.

Let's create a text file with 100K lines to replace.

$ time ( for i in {1..100000}; do echo "foobar" >> replacetext.txt; done )

real    2m23.845s
user    0m9.142s
sys     0m17.628s



Now let's create a vim script file with the substitution commands.

$ vim -W replace.script replacetext.txt

Type the following
:%s/foobar/barfoo/
:wq


I do this because I'll definitely mess up the end-of-line characters, I prefer to let vim's "-W" switch create the script file for me.

Let's start the race!

$ time sed -i 's/barfoo/foobar/' replacetext.txt

real    0m0.470s
user    0m0.249s
sys     0m0.202s

$ time vim -s replace.script replacetext.txt

real    0m0.320s
user    0m0.265s
sys     0m0.046s



Vim wins!

Next time I'm bored: race different methods to create that text file. This for-loop took for ever.