簡體   English   中英

為什么gprof告訴我從main()僅調用一次的函數被調用102次?

[英]Why does gprof tell me that a function that is called only once from main() is called 102 times?

我是一個初學者,為了有趣,編寫了以下程序,以搜索目錄並將每個出現的單詞替換為另一個單詞。 我只一次調用了crt_ls_file()函數,但是gprof告訴我它被調用了102次。 我想知道是否有人知道這是為什么。 我已經嘗試過編譯程序,並且不會進行任何優化。

#include <iostream>
#include <string>
#include <cstdlib>
#include <cassert>
#include <fstream>
using namespace std;

void crt_ls_file(const string& output_f, const string& dir);
void sed(const string& old_string, const string& new_string, const string& filename, const string& directory);

int main(int argc, char* argv[]){

    string out_f;
    if  (argc <= 1) {
        cout << "Usage: " << argv[0] << " <Filename>" << endl;
        exit(EXIT_FAILURE);
    } else {
        out_f =  ".sandr";
        crt_ls_file(out_f, string(argv[1]) );
    }

    ifstream out_fs( out_f.c_str() );
    string line;
    getline(out_fs, line);
    while( !out_fs.eof() ){
        sed(string("index_SYMBOL"), string("index1_SYMBOL"), line, string(argv[1]) );
        getline(out_fs, line);
    }
    out_fs.close();
    string f( "rm " + out_f );
    system ( f.c_str() );

    exit(EXIT_SUCCESS);
}

void crt_ls_file(const string& s, const string& a){
    ofstream ls( s.c_str() );
    ls.close();
    string ls_output( "ls -1 " + a + " > ./" + string(s) );
    system( ls_output.c_str() );
}

void sed(const string& o, const string& n, const string& f, const string& d){
    ofstream dummy(".temp");
    dummy.close();

    string sed_output( "sed 's/" + o + "/" + n + "/g' " + d + "/" + f + " > .temp" );
    system( sed_output.c_str() );
    string rp( "mv .temp " + d + "/" + f );
    system ( rp.c_str() );
}

在我的系統上, gprof僅顯示對crt_ls_file一個調用,因為它應該是:

  0.00      0.00     0.00        1     0.00     0.00  crt_ls_file(std::string const&, std::string const&)

因此,似乎您的gprof在說謊,有時也是如此。 如果您真的想對此程序進行概要分析(幾乎沒有用),請嘗試使用callgrind和kcachegrind。 它們是更好而更少的神秘工具:

$ valgrind --tool=callgrind ./my_program some_dir
... let it do its job ...
$ kcachegrind

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM