English 中文(简体)
准确衡量第二代执行时间
原标题:Accurately measuring execution time in seconds
  • 时间:2012-04-25 12:35:26
  •  标签:
  • c
  • time

我想知道方案的执行时间,但我也想排除提交投入/产出所需的时间。 为此,我使用了<代码>时间()的功能。 有许多<代码>.h和.cpp文档,但这里是唯一提到时间标的的地方。

我的问题是,虽然开始计算价值从一开始就是正确的,但在第一次援引<代码>繁琐的<<>代码/代码>时,它就失去了价值。 这使得<代码>下>(耐久<>/代码>)具有很大价值,因为现在<代码>end<>/code>和之间的差额(0)为<代码>end

这里是我如何使用:

主 席

time_t start=0;
time_t end=0;
time_t sub_duration=0;
time_t total_duration=0; 

int main()
{
    start = time(NULL);
    while(somethingtodo)
    {
        dosomething();
        dosomethingelse();
    }
    end = time(NULL);
    sub_duration = difftime(end,start);
    total_duration += sub_duration;
}

do。

   extern      time_t start;
   extern      time_t end;
   extern      time_t sub_duration;
   extern      time_t total_duration;
   dosomethingelse();

do

#include" do。"    
dosomethingelse()
{
    if(somecondition)
    {
        end = time(NULL);
        sub_duration = difftime(end,start);
        total_duration += sub_duration;
        writesomethingTofile();
        start = time(NULL);
    }
}
最佳回答

问题在于<条码> 启动/编码>和<条码>。 当你在<条形码>内将其定位时,你重新制定整个方案<条码><>>>>>。

解决办法是在<条形码>内保持当地时间,并将这一功能所用时间从总数中减去。

问题回答

« start » is changed into « main » and « dosomethingelse » functions. Why did you do that ?





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签