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 636173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:29:10+00:00 2026-05-13T20:29:10+00:00

I have a c++ program which performs one function. It loads a large data-file

  • 0

I have a c++ program which performs one function. It loads a large data-file into an array, receives an array of integers and performs a lookup in that array, returning a single integer. I am currently calling the program with each integer as an argument, like so:

$ ./myprogram 1 2 3 4 5 6 7

I also have a ruby script, and I would like this script to utilize the c++ program.
Currently, I am doing this like so.

Ruby Code:

arguments = "1 2 3 4 5 6 7"
an_integer = %x{ ./myprogram #{arguemnts} }
puts "The program returned #{an_integer}" #=> The program returned 2283

This is all working properly, but my problem is that each time ruby makes this call, the c++ program has to reload the data-file (which is over 100mb) – very slow, and very inefficient.

How can I rewrite my c++ program load the file only once, allowing me to make many lookups via a ruby script without reloading the file each time. Would using sockets be a sensible approach? Writing the c++ program as a ruby extension?

Obviously I am not an experienced c++ programmer, so thanks for your help.

  • 1 1 Answer
  • 2 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-13T20:29:11+00:00Added an answer on May 13, 2026 at 8:29 pm

    A possible approach is to modify your C++ program so that it takes its input from the standard input stream (std::cin) instead of from the command line parameters, and returns its result through the standard ouput (std::cout) instead of as main’s return value. Your Ruby script would then use popen to launch the C++ program.

    Assuming the C++ program currently looks like:

    // *pseudo* code
    int main(int argc, char* argv[])
    {
        large_data_file = expensive_operation();
    
        std::vector<int> input = as_ints(argc, argv);
        int result = make_the_computation(large_data_file, input);
    
        return result;
    }
    

    It would be transformed into something like:

    // *pseudo* code
    int main(int argc, char* argv[])
    {
        large_data_file = expensive_operation();
    
        std::string input_line;
        // Read a line from standard input
        while(std:::getline(std::cin, input_line)){
            std::vector<int> input = tokenize_as_ints(input_line);
            int result = make_the_computation(large_data_file, input);
    
            //Write result on standard output
            std::cout << result << std::endl;
        }
    
        return 0;
    }
    

    And the Ruby script would look like

    io = IO.popen("./myprogram", "rw")
    while i_have_stuff_to_compute
        arguments = get_arguments()
        # Write arguments on the program's input stream
        IO.puts(arguments)
        # Read reply from the program's output stream
        result = IO.readline().to_i();
    end
    
    io.close()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a high-speed ADC data capture/analysis program which performs poorly on older computers.
I have a very old, very very large, fully working, C program which plays
I have a PHP script which reads a large CSV and performs certain actions,
I have a lovely AppleScript droplet which performs OCR of a PDF file using
In some crunching number program, I have a function which can be just 1
I have a program which needs to invoke a process to perform an operation
I have a program which creates JButtons which are then added to a JPanel
I have a program which dynamically generates a GUI. I don't know how many
I have a program which outputs a pair of words surrounded by spaces on
I have a program which prints a multi-page document. The first page is pre-printed

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.