C++11 C++;ScopeTimer不';行不通
我试图实现一个计时器类,它打印给定范围所需的时间。不知怎的,我不能让它正常工作。到目前为止,我的代码是: Main.cpp:C++11 C++;ScopeTimer不';行不通,c++11,timer,scope,C++11,Timer,Scope,我试图实现一个计时器类,它打印给定范围所需的时间。不知怎的,我不能让它正常工作。到目前为止,我的代码是: Main.cpp: #include "scopetimer.hpp" #include <cstdlib> #include <cmath> #include <string> #include <chrono> #include <iostream> void work01() {
#include "scopetimer.hpp"
#include <cstdlib>
#include <cmath>
#include <string>
#include <chrono>
#include <iostream>
void work01()
{
double numbers[10000];
for (int i = 0; i < 10000; ++i)
{
numbers[i] = double(std::rand()) / double(RAND_MAX);
}
for (int n = 10000; n > 1; n = n - 1) {
for (int i = 0; i < n - 1; i = i + 1) {
if (numbers[i] > numbers[i + 1]) {
double tmp = numbers[i];
numbers[i] = numbers[i + 1];
numbers[i + 1] = tmp;
}
}
}
}
void work02()
{
int* buf[1024];
for (int i = 2; i < 1024; ++i)
buf[i] = new int[i];
for (int i = 2; i < 1024; ++i)
delete[] buf[i];
}
// counts the number of primes in an interval
int work03(int n0, int n1)
{
int freq = n1 - n0 + 1;
for (int i = n0; i <= n1; ++i)
{
// Have fun: use the alternative iteration direction and see how fast
// it gets!
// for(int j = 2; j < i; ++j)
for (int j = i - 1; j > 1; --j)
{
if (i%j == 0)
{
--freq;
break;
}
}
}
return freq;
}
int main(int, char**)
{
{ ScopeTimer("work01");
work01();
}
{
ScopeTimer("work02");
work02();
}
{
ScopeTimer("work03");
work03(0, 10000);
}
std::cout << std::endl << "Tests" << std::endl << std::endl;
{
clock_t start_(std::clock());
work01();
clock_t end_(std::clock());
std::cout << "Test Timer: " << end_ - start_ << "ns" << std::endl;
}
{
clock_t start_(std::clock());
work02();
clock_t end_(std::clock());
std::cout << "Test Timer: " << end_ - start_ << "ns" << std::endl;
}
{
clock_t start_(std::clock());
work03(0,10000);
clock_t end_(std::clock());
std::cout << "Test Timer: " << end_ - start_ << "ns" << std::endl;
}
system("Pause");
}
scopetimer.cpp
#include "scopetimer.hpp"
#include <cmath>
#include <string>
#include <chrono>
#include <iostream>
ScopeTimer::ScopeTimer(const std::string& name)
:name_(name),
start_(std::clock()) {
}
ScopeTimer::~ScopeTimer() {
double elapsed = (double(std::clock() - start_) / double(CLOCKS_PER_SEC));
std::cout << name_ << ": " << int(elapsed) << "ns" << std::endl;
}
#包括“scopetimer.hpp”
#包括
#包括
#包括
#包括
#包括
无效工作01()
{
双倍数字[10000];
对于(int i=0;i<10000;++i)
{
数字[i]=double(std::rand())/double(rand_MAX);
}
对于(int n=10000;n>1;n=n-1){
对于(int i=0;i数字[i+1]){
双tmp=数字[i];
数字[i]=数字[i+1];
数字[i+1]=tmp;
}
}
}
}
无效工作02()
{
int*buf[1024];
对于(int i=2;i<1024;++i)
buf[i]=新整数[i];
对于(int i=2;i<1024;++i)
删除[]buf[i];
}
//计算一个区间内的素数
int work03(int n0,int n1)
{
int freq=n1-n0+1;
对于(int i=n0;i 1;--j)
{
如果(i%j==0)
{
--频率;
打破
}
}
}
返回频率;
}
int main(int,char**)
{
{ScopeTimer(“work01”);
work01();
}
{
范围计时器(“work02”);
work02();
}
{
范围计时器(“工作03”);
工作03(0,10000);
}
std::cout在~ScopeTimer()
中,您可以打印经过的完整秒数,而不是多少纳秒,而在main
的第二部分,您可以打印时钟节拍数,它可能与纳秒相同,也可能与纳秒不同。我遇到了同样的问题
我的解决方案是定义ScopeTimer实例,而不是仅仅调用其构造函数,我的意思是:
{
ScopeTimer _scopetimer("work01");
work01();
}
这应该行得通
我猜想编译器似乎忽略(优化),因为当你只调用ScopeTimer(“Work01”)。< /p>不回答你的问题:<代码>时钟()/代码>是C函数。如果你想要C++,你最好看<代码> <代码>。