Fold. End of story.

Finally. That took only four posts and several hours. Prototyping in familiar language to get grip onto the algorithm totally worth it, at least right now, while i still noob in C. Here is how my fold.php compares to my fold.c to local Mac OS fold command:

$ time cat big.txt | php fold.php 12
real	0m22.143s
user	0m8.559s
sys	0m2.682s
$ time cat big.txt | ./fold
real	0m7.495s
user	0m2.101s
sys	0m1.183s
$ time cat big.txt | fold -w 12
real	0m5.037s
user	0m1.659s
sys	0m0.959s

There are obviously optimizations to be made, will see later. Also i am using default compiler settings, whatever difference that makes. The “big.txt” file is this one, 6.2Mb.

Here’s the source code: fold.c.

That’s it for today.