4

我有一个执行一个功能的 c++ 程序。它将一个大数据文件加载到一个数组中,接收一个整数数组并在该数组中执行查找,返回一个整数。我目前正在使用每个整数作为参数调用程序,如下所示:

$ ./myprogram 1 2 3 4 5 6 7

我还有一个 ruby​​ 脚本,我希望这个脚本能够利用 c++ 程序。目前,我正在这样做。

红宝石代码:

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

这一切正常,但我的问题是每次 ruby​​ 进行此调用时,c++ 程序都必须重新加载数据文件(超过 100mb)——非常慢,而且效率非常低。

如何重写我的 c++ 程序只加载一次文件,允许我通过 ruby​​ 脚本进行多次查找,而无需每次都重新加载文件。使用套接字是一种明智的方法吗?将 c++ 程序编写为 ruby​​ 扩展?

显然我不是一个经验丰富的 C++ 程序员,所以感谢您的帮助。

4

2 回答 2

5

一种可能的方法是修改您的 C++ 程序,使其从标准输入流 (std::cin) 而不是命令行参数中获取输入,并通过标准输出 (std::cout) 而不是返回其结果作为 main 的返回值。然后,您的 Ruby 脚本将使用 popen 来启动 C++ 程序。

假设 C++ 程序当前看起来像:

// *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;
}

它将被转换成类似的东西:

// *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;
}

Ruby 脚本看起来像

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()
于 2010-02-18T13:52:41.057 回答
3

出色地,

你可以通过多种不同的方式来解决这个问题。

1)一个简单的,可能丑陋的方法是让你的c ++运行并间歇性地检查一个文件,让你的ruby脚本生成包含你的参数的文件。然后,您的 C++ 程序将使用包含的参数将其结果返回到结果文件中,您可以在 ruby​​ 脚本中等待该文件......这显然是 HACK TASTIC,但它实现起来非常简单并且可以工作。

2) 将您的 c++ 代码作为 ruby​​ 的 ac 扩展公开。这并不像听起来那么难,特别是如果您使用RICE并且会提供更少的 hackie 解决方案。

3) 如果你的 c++ 可以通过 ac 头文件公开,那么使用 FFI 构建桥几乎是微不足道的。Jeremy Hinegardner 在 ruby​​conf 就构建 FFI 接口做了一个很好的演讲,这里是截屏视频

4) D-Bus提供应用程序通信总线,您可以更改您的 C++ 应用程序以利用所述事件总线并使用ruby​​-dbus从您的 ruby​​ 传递消息

当然还有一千条其他路线......也许其中一条或另一条可以证明是可行的:)

干杯!

于 2010-02-18T14:00:48.053 回答