4 ms·
Hey Jacques, may ask for a code review ? :-) http://stdio.be/revseq.c http://stdio.be/revseq.c Compiled with "gcc -pipe -Wall -O3 -fomit-frame-pointer -std=c9
by ay 14y ago
Hey Jacques, may ask for a code review ? :-)
http://stdio.be/revseq.c http://stdio.be/revseq.c
Compiled with "gcc -pipe -Wall -O3 -fomit-frame-pointer -std=c99 -pthread" on my Mac, it's about twice as fast at the blog author's version.
Time spent:
coding: 30 minutes
bugfixing: 30 minutes
I have a feeling there is some kind of catch in the description of the algorithm in terms of implementing the output, but I for the life of me could not grok whether they wanted me to parse the entry into these three pieces or not...
EDIT to add: The only optimization I made was inlining the tightly-called "subst" function, did it without any profiling (so the optimization process literally took about 30 seconds:). Before inlining this version was still about 15% faster than the blog author's one.
- jacquesm 14y agoSure, I'll pick it up in the post. Neat little project this. I won't peek at your code until I'm done.
- ay 14y agoThank you! It was indeed a nice little fun exercise. It is interesting the bugs that I made by being tired and not reading the task carefully/not thinking clearly (yesterday was a bit of a long and stressy day): 1) My initial understanding was that I do need to reverse the order, yet somehow after re-reading the article I understood the order does not need to change, and the "reverse" in the name is some kind of jargon. This is quite stupid, and probably not worth mentioning, if only to prove I was tired :) 2) missing that the first iteration of the "business logic" code in my case happens before anything is filled in. Crash. 3) forgetting about the "\n"s - with rather funny "partially correct" output effect. Very much looking forward to see your code !
- jacquesm 14y agoHey Andrew, Ok, I looked at your code. What you really should do (before coding up the solution) is to look at the problem specification. Other than that I like the 'direct' approach, it isn't quite as fast as what I cooked up but yours is a lot shorter.
- ay 14y agoThanks! yes, this goes to show the perils of coding after a 12h work day on Friday :-). This affected the use of fgets() I/O (I understood you have to call the line-buffered routines based on their description) Enjoyed reading your code. Beautiful. Thanks!