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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:18:37+00:00 2026-06-11T04:18:37+00:00

Searched but didn’t seem to find exactly what I needed. I’m looking for a

  • 0

Searched but didn’t seem to find exactly what I needed.

I’m looking for a fast C++ equivalent of running the following command:

diff file1.log file2.log | wc -l

At present, I’m using file pipes in order to run diff from the command line, however I need to do this in a large, multi-nested loop and it takes quite longer than I anticipated. The files being diffed are roughly 150-200mb each, and each diff takes roughly 1-2 minutes.

Is there a faster solution that can be rolled by C++?

Here is my present method of calling it:

static std::string run_cmd(std::string in)
{
  // run command
  FILE* pipe = popen(in.c_str(), "r");
  if (!pipe)  return "err";

  char buff[128];
  std::string res = "";
  while (!feof(pipe))
  {
    if (fgets(buff, 128, pipe) != NULL)
      res += buff;
  }
  pclose(pipe);
  return res;
}

// diff 2 given files and return line number differences
std::string fileDiff(std::string file1, std::string file2)
{
  std::string f1 = base + file1;
  std::string f2 = base + file2;
  std::string cmd = "diff " + f1 + " " + f2 + " | wc -l";

  std::string res = run_cmd(cmd);
  if (res == "err") 
    return "E: Diff on [" + f1 + "] and [" + f2 + "]";

  return res;
}

Edit:

What I am essentially doing is logging code coverage. I’ve inserted logging statements into each nook and cranny of the codebase I’m working in, and writing each run to its own log file. I’ve attempted to minimize the writing penalty by not including them in constructors, loops, etc, and have buffered the actual writing process.

The program I had typically took about 10 minutes to run. With my added logging and diff calls its scaled up to about ~1 day.

I only care about the amount of line differences in this case, as it is feeding a fitness function in a genetic algorithm. The spread of execution paths between iterations is important at this point.

  • 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-06-11T04:18:39+00:00Added an answer on June 11, 2026 at 4:18 am

    Launching an external process is fast. At 1-2 minutes per file, the process spawn overhead is a tiny insignificant fraction. You must be limited by 1) the performance of the diff command or 2) inefficient reading and storing of the pipe’s output data. Try running the diff command in the shell and outputting into a file. Is it much faster? If not, then 1). If so, then 2).

    I don’t know much about Unix pipes, but a 128-byte buffer sounds small. The diff command is old and widely used, so it’s unlikely that you could write a faster version.

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

Sidebar

Related Questions

Well, I looked and searched but didn't find what I was looking for exactly,
I have tried and searched but didn't find... How can I change the following
I searched everywhere but didn't find the solution. I have image 1. How can
I searched around a little bit for information on this but didn't find anything
I searched the registry. I'd expect the settings in HKEY_CURRENT_USER\Software\CodeGear\BDS\7.0 but didn't find anything.
I searched pretty much everywhere on google, but I didn't find anything useable. I
I have searched on SO for similar questions but didn't find any.So forgive me
I googled and searched in the boost's man, but didn't find any examples. May
Hi I searched but i didn't find such solution.Is it possible to create a
I have searched a lot but didn't find it. so sorry if this is

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.