C ++中函数的执行时间

时间:2017-04-17 22:03:59

标签: c++

我想使用几个声明相同数组但以不同方式(静态地,在堆栈上和堆上)并显示每个函数的执行时间的函数。最后我想多次调用这些函数。

我想我已经设法做了所有事情,但是对于函数的执行时间我经常得到0并且我不知道它是否应该是正常的。如果有人能为我确认。谢谢 这是我的代码

#include "stdafx.h"
#include <iostream>
#include <time.h>
#include <stdio.h>
#include <chrono>

#define size 100000
using namespace std;

void prem(){
  auto start = std::chrono::high_resolution_clock::now();
  static int array[size];
  auto finish = std::chrono::high_resolution_clock::now();
  std::chrono::duration<double> elapsed = finish - start;

  std::cout << "Elapsed timefor static: " << elapsed.count() << " s\n";
}

void first(){
  auto start = std::chrono::high_resolution_clock::now();
  int array[size];
  auto finish = std::chrono::high_resolution_clock::now();
  std::chrono::duration<double> elapsed = finish - start;
  std::cout << "Elapsed time on the stack: " << elapsed.count() << " s\n";
}

void secon(){
  auto start = std::chrono::high_resolution_clock::now();
  int *array = new int[size];
  auto finish = std::chrono::high_resolution_clock::now();
  std::chrono::duration<double> elapsed = finish - start;
  std::cout << "Elapsed time dynamic: " << elapsed.count() << " s\n";

  delete[] array;
}

int main()
{
  for (int i = 0; i <= 1000; i++){
    prem();
    first();
    secon();
  }
  return 0;
}

2 个答案:

答案 0 :(得分:2)

prem() - 数组在函数
之外分配 first() - 数组在代码到达之前分配

您在一个循环中循环所有3个函数。为什么?你不是要分别在1000次循环每个人,这样他们(希望)不会相互影响吗?实际上,最后一句话并非如此。

我的建议:

  1. 分别循环每个功能
  2. 对整个now()循环进行1000调用:在进入循环之前进行now()调用,然后在退出循环之后,获取差异并将其除以数字迭代次数(1000
  3. 动态分配可以(通常)简化为只是在广阔的可用地址空间中抓取一块内存(我假设您在64位平台上运行),除非您实际使用那个内存,操作系统甚至不需要确保它在RAM中。这肯定会严重扭曲你的结果
  4. 写一个&#34;驱动程序&#34;获取函数指针的函数&#34; test&#34;
  5. 可能实现driver()函数:

    void driver( void(*_f)(), int _iter, std::string _name){
      auto start = std::chrono::high_resolution_clock::now();
      for(int i = 0; i < _iter; ++i){
        *_f();
      }
      auto finish = std::chrono::high_resolution_clock::now();
      std::chrono::duration<double> elapsed = finish - start;
      std::cout << "Elapsed time " << _name << ": " << elapsed.count() / _iter << " s" << std::endl;
    }
    

    那样你的main()就是这样:

    void main(){
      const int iterations = 1000;
      driver(prem, iterations, "static allocation");
      driver(first, iterations, "stack allocation");
      driver(secon, iterations, "dynamic allocation");
    }
    

答案 1 :(得分:1)

不要进行此类综合测试,因为编译器会优化未使用的所有内容。

如另一个答案所示,您需要测量整个1000循环的时间。即便如此,我认为你不会得到合理的结果。

让我们不进行1000次迭代,而不是1000000次。让我们添加另一种情况,我们只需要对chrono::high_resolution_clock::now()进行两次后续调用作为基线:

#include <iostream>
#include <time.h>
#include <stdio.h>
#include <chrono>
#include <string>
#include <functional>

#define size 100000
using namespace std;

void prem() {
    static int array[size];
}

void first() {
    int array[size];
}

void second() {
    int *array = new int[size];
    delete[] array;
}

void PrintTime(std::chrono::duration<double> elapsed, int count, std::string msg)
{
    std::cout << msg << elapsed.count() / count << " s\n";
}

int main()
{
    int iterations = 1000000;
    {
        auto start = std::chrono::high_resolution_clock::now();
        auto finish = std::chrono::high_resolution_clock::now();
        PrintTime(finish - start, iterations, "Elapsed time for nothing: ");
    }
    {
        auto start = std::chrono::high_resolution_clock::now();
        for (int i = 0; i <= iterations; i++)
        {
            prem();
        }
        auto finish = std::chrono::high_resolution_clock::now();
        PrintTime(finish - start, iterations, "Elapsed timefor static: ");
    }
    {
        auto start = std::chrono::high_resolution_clock::now();
        for (int i = 0; i <= iterations; i++)
        {
            first();
        }
        auto finish = std::chrono::high_resolution_clock::now();
        PrintTime(finish - start, iterations, "Elapsed time on the stack: ");
    }
    {
        auto start = std::chrono::high_resolution_clock::now();
        for (int i = 0; i <= iterations; i++)
        {
            second();
        }
        auto finish = std::chrono::high_resolution_clock::now();
        PrintTime(finish - start, iterations, "Elapsed time dynamic: ");
    }
    return 0;
}

通过所有优化,我得到了这个结果:

Elapsed time for nothing: 3.11e-13 s
Elapsed timefor static: 3.11e-13 s
Elapsed time on the stack: 3.11e-13 s
Elapsed time dynamic: 1.88703e-07 s

这基本上意味着,编译器实际上优化了prem()first()。即使不是电话,也不是整个循环,因为它们没有副作用。