benchmarking linux and C++ program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Man4ish
    New Member
    • Mar 2008
    • 151

    benchmarking linux and C++ program

    I am benchmarking linux and C++ program to compare the results. I am testing wc -l command and C++ program doing the same but linux command wc -l is much faster than c++ progarm. Why?

    #include <iostream>
    #include <fstream>
    using namespace std;
    int main(int argc, char* argv[])
    {
    ifstream in(argv[1]);
    char line[1000];
    int count =0;
    while(in)
    {
    in.getline(line ,1000);
    count++;
    }
    in.close();
    cout << count <<endl;
    return 0;
    }
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    HI,
    The Linux commands will be much more optimized than your code and that may be one of the reason for that.
    Thanks
    Raghu

    Comment

    • Man4ish
      New Member
      • Mar 2008
      • 151

      #3
      How can i learn such a great optimization techniques for speeding up my programs.

      Comment

      • Man4ish
        New Member
        • Mar 2008
        • 151

        #4
        How can i learn such a great optimization techniques for speeding up my programs?

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          You might not be able to but before we go there I have to ask did you compile your program with optomisation preferably speed optomisation. Normally if you compiled it for debug optomisation will be switched off. This would also explain the speed difference.

          If you had optomisation on or having switch it on wc still goes faster one of the reasons is that it is likely that wc is making system calls directly. It probably benifits from being able to call the system directly being a part of the system where as your program is using standard C++ and is removed from the system by several layers.

          If you still want further information then I suggest that you try looking at the source code for wc and see how they did it. This thread on the Ubuntu forums explains how to get that source code.

          Comment

          Working...