Tag Archives: fold

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.

Fold. Not the end.

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
real 0m0.179s
user 0m0.151s
sys 0m0.017s

$ time fold -w 10 syntax.text
real 0m0.041s
user 0m0.010s
sys 0m0.010s

Right…

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:
He is just charging his iPhone.

Fold and beyond

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
Pathetic.

That’s it for today.

C

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 `!!`?