I am looking for optimisation ideas regarding the below piece of code written in Ruby. It looks for header file dependencies in a dependency file (generated by GCC using gcc -M -MM -MF)
I’ve ran the Ruby profiler and found that most of the CPU time was spent in the Array#each function below.
lines = File.readlines(depends_file)
lines.each do |line|
if line.include?(".h") then
line.lstrip.rstrip.chomp("\\").split(' ').each do |line|
header_dependencies << line
end
end
end
The depends file contains dependencies in the makefile rule format as specified by gcc’s -M option, see GCC’s Preprocessor Options. Here’s an example from my project –
CyclicRedundancyCheck.o: src/CyclicRedundancyCheck.c \
inc/CyclicRedundancyCheck.h inc/StdDefs.h
Can someone please post an alternate, heavily optimised solution?
Not heavily optimized but a bit faster here (about 20% for my test-case):
I don’t think you can gain much more with Ruby itself. If this is really too slow, you should consider programming it (partly) in C (as an extension).
Note also that the approach is far from being robust. E.g.
will wrongly be matched. Making it more robust will most likely cost performance, though.