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
$ time cat big.txt | ./fold
$ time cat big.txt | fold -w 12
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.
Really lazy weekend. Barely touched a keyboard, maybe just to chat for some time.
Still. I have rewritten `fold` in PHP, which is complete cheating, but at least i know how it should work now. Next step – implement it in C. And this is not because I am learning C, but because of this:
$ time php fold.php 10 syntax.text
$ time fold -w 10 syntax.text
PHP is not fast at all. But i have spent 20 minutes hacking that piece of code together and it totally works. That’s why dynamic languages rule. I still want to learn C, so have to rewrite that again.
Also, here is the picture of a guy stealing electricity on 42nd-Bryant Park:
So today i was writing a post about memory leaks and how they can be very frustrating. The whole topic is very big, obviously, so even writing a small post is taking more time than expected. Apart from that i was wrestling with fold.c from yesterday. My problem there are not memory leaks, but mostly a lack of understanding how that thing should work. I have to take a pen and piece of paper and just make it work that way first.
Still looking for a decent, quick-to-learn testing solution for making sure my C code works. I very well may have to come up with a solution myself. But since i am too lazy, i very well may not.
Right now my testing process looks like this:
rm a.out ; gcc fold.c; echo "thisisreallylongstring" | ./a.out
That’s it for today.
Some time ago i bought a classical C manual: “The C Programming Language” by Kernighan and Ritchie. Good read.
Today i was implementing “fold” from the book exercises. It is not coming easy this time. I have some ideas on how to progress, but right now i am stuck and in the bar somewhere in Williamsburg. Obviously trying to reach Ballmer’s Peak.
Two things for tomorrow:
- Need automated testing;
- Algorithms matter;
Since no finished code on hands today – i am not posting it here.
P.S. did you know that bash can repeat last executed command with all parameters if you run `!!`?