Re:Interviewing programmers: coding test example explained
by nkurz on Friday June 04, 2010 @12:14AM
(#1403468)
This might be a case where one should benchmark before making changes that make the code more complex. I just threw your code your code into a thrown together cycle counter, and on my system it performs significantly worse than the straightforward solution.
My test harness isn't perfect, but with "gcc -O3" on a Core i7, I get something like 20000 cycles to 'condense' a 4096 character random string with the suggested loop, versus 26000 with your more complex approach.
With a 4096 character string set up to never contain char_to_remove, I get a practically identical 19500 cycles with both approaches.
Thus there seems to be no benefit to the greater complexity.
My test harness isn't perfect, but with "gcc -O3" on a Core i7, I get something like 20000 cycles to 'condense' a 4096 character random string with the suggested loop, versus 26000 with your more complex approach.
With a 4096 character string set up to never contain char_to_remove, I get a practically identical 19500 cycles with both approaches.
Thus there seems to be no benefit to the greater complexity.