Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6898789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:20:27+00:00 2026-05-27T07:20:27+00:00

I am looking for optimisation ideas regarding the below piece of code written in

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T07:20:28+00:00Added an answer on May 27, 2026 at 7:20 am

    Not heavily optimized but a bit faster here (about 20% for my test-case):

    File.open(depends_file, 'r') do | io |
      io.each do |line|
        next unless line.include?('.h')
        header_dependencies.concat(line.split)
      end
    end
    header_dependencies.delete('\\')
    

    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.

     foo.o: src/ext.hlib/foo.c \
    

    will wrongly be matched. Making it more robust will most likely cost performance, though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using LinqPad and in that I was looking at the IL code(compiler
We are looking for some tips over a performance and search engine optimisation (SEO)
before I start I want to clarify that I am not looking for code
I'm looking into speeding up my python code, which is all matrix math, using
I'm looking for ideas for a heap-manager to handle a very specific situation: Lots
All, I'm looking up a zip code and then finding all the other zip
I'm looking for some general Optimization Correctness Extensibility advice on my current C++ Hierarchical
Looking for a good set of base objects to start a website up in
Looking to move data from a table A to history table B every X
Looking for advice on framework and approach for building a modular web application. Primary

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.