From: Ryan Davis Date: 2011-04-19T04:15:19+09:00 Subject: Re: Need for speed -> a C extension? On Apr 18, 2011, at 11:30 , Martin Hansen wrote: > WJ wrote in post #993576: >> Martin Hansen wrote: >> >>> The below code is too slow for practical use. I need it to run at least >>> 20 times faster. Perhaps that is possible with some C code? I have no >>> experience with writing Ruby extensions. What are the pitfalls? Which >>> part of the code should be ported? Any pointers to get me started? >> >> Please give a clear description of the algorithm, and then >> give some sample input and output. > > > Here is a working version of the code that can be profiled (though it > will take forever with 20M iterations): > > http://pastie.org/1808127 > > The slow part according to profiler is: Profiling is great, but you also need to write a benchmark in order to objectively measure the work you're doing. You might also want to write microbenchmarks against the bottlenecks you identify in your profiling work. Eg: > def match?(char1, char2) > (EQUAL[char1.upcase.ord] & EQUAL[char2.upcase.ord]) != 0 > end That would be really easy to write tests and a benchmark against and then rewrite in C using RubyInline... without tests and a benchmark tho, you won't know that you've done it correctly and provides a measurable benefit.