我希望计算API返回值所花费的时间.这种行动所花费的时间是纳秒秒.由于API是C++类/函数,我使用timer.h来计算相同的:
#include <ctime>
#include <cstdio>
using namespace std;
int main(int argc, char** argv) {
clock_t start;
double diff;
start = clock();
diff = ( std::clock() - start ) / (double)CLOCKS_PER_SEC;
cout<<"printf: "<< diff <<'\n';
return 0;
}
Run Code Online (Sandbox Code Playgroud)
上面的代码给出了以秒为单位的时间.如何在毫秒秒内获得相同的精度?
计算C++时差的最佳方法是什么?我正在计算程序的执行速度,所以我对毫秒感兴趣.更好的是,秒.毫秒..
接受的答案有效,但需要包括ctime或time.h,如评论中所述.
我一直用clock()来测量我的应用程序从开始到结束的时间,因为;
int main(int argc, char *argv[]) {
const clock_t START = clock();
// ...
const double T_ELAPSED = (double)(clock() - START) / CLOCKS_PER_SEC;
}
Run Code Online (Sandbox Code Playgroud)
由于我已经开始使用POSIX线程,这似乎失败了.看起来clock()用N个线程增加N倍.由于我不知道将同时运行多少个线程,因此这种方法失败了.那么如何衡量已经过了多少时间呢?
我想测量我的C++代码的运行时.执行我的代码大约需要12个小时,我想在执行代码时写下这段时间.我怎么能在我的代码中做到这一点?
操作系统:Linux
我试图选择一种将积分转换为字符串的标准方法,所以我接着通过测量3种方法的执行时间进行了一次小的性能评估
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <chrono>
#include <random>
#include <exception>
#include <type_traits>
#include <boost/lexical_cast.hpp>
using namespace std;
// 1. A way to easily measure elapsed time -------------------
template<typename TimeT = std::chrono::milliseconds>
struct measure
{
template<typename F>
static typename TimeT::rep execution(F const &func)
{
auto start = std::chrono::system_clock::now();
func();
auto duration = std::chrono::duration_cast< TimeT>(
std::chrono::system_clock::now() - start);
return duration.count();
}
};
// -----------------------------------------------------------
// 2. Define the convertion functions …Run Code Online (Sandbox Code Playgroud) 我想知道为什么以下代码无法编译:
struct S
{
template <typename... T>
S(T..., int);
};
S c{0, 0};
Run Code Online (Sandbox Code Playgroud)
此代码无法使用clang和GCC 4.8进行编译.这是clang的错误:
test.cpp:7:3: error: no matching constructor for initialization of 'S'
S c{0, 0};
^~~~~~~
test.cpp:4:5: note: candidate constructor not viable: requires 1 argument, but 2 were provided
S(T..., int);
^
Run Code Online (Sandbox Code Playgroud)
在我看来,这应该工作,T应该被推断为一个长度为1的包.
如果标准禁止做这样的事情,有谁知道为什么?
我有一个非常大的文本文件(45GB).文本文件的每一行包含两个空格分隔的64位无符号整数,如下所示.
4624996948753406865 10214715013130414417
4305027007407867230 4569406367070518418
10817905656952544704 3697712211731468838 ......
我想读取文件并对数字执行一些操作.
void process_data(string str)
{
vector<string> arr;
boost::split(arr, str, boost::is_any_of(" \n"));
do_some_operation(arr);
}
int main()
{
unsigned long long int read_bytes = 45 * 1024 *1024;
const char* fname = "input.txt";
ifstream fin(fname, ios::in);
char* memblock;
while(!fin.eof())
{
memblock = new char[read_bytes];
fin.read(memblock, read_bytes);
string str(memblock);
process_data(str);
delete [] memblock;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我对c ++比较陌生.当我运行此代码时,我遇到了这些问题.
由于以字节读取文件,有时块的最后一行对应于原始文件中的未完成行("4624996948753406865 10214"而不是主文件的实际字符串"4624996948753406865 10214715013130414417").
这段代码运行得非常慢.在具有6GB RAM的64位Intel Core i7 920系统中运行一个块操作需要大约6秒.是否有任何可用于改善运行时的优化技术?
是否有必要在boost分割功能中包含"\n"和空白字符?
我已经阅读了关于C++中的mmap文件,但我不确定这是否是正确的方法.如果是,请附上一些链接.
以下代码无法编译:
#include <iostream>
template<typename F, typename ...Args>
static auto wrap(F func, Args&&... args)
{
return func(std::forward<Args>(args)...);
}
void f1(int, char, double)
{
std::cout << "do nada1\n";
}
void f2(int, char='a', double=0.)
{
std::cout << "do nada2\n";
}
int main()
{
wrap(f1, 1, 'a', 2.);
wrap(f2, 1, 'a');
}
Run Code Online (Sandbox Code Playgroud)
g++ -std=c++14 -O2 -Wall -pedantic -pthread main.cpp && ./a.out
main.cpp: In instantiation of 'auto wrap(F, Args&& ...) [with F = void(*)(int, char, double); Args = {int, char}]':
main.cpp:22:20: required from …Run Code Online (Sandbox Code Playgroud) 我在Effective Modern C++中看到的代码片段巧妙地实现了创建函数计时器的工具原理:
auto timeFuncInvocation =
[](auto&& func, auto&&... params)
{
start timer;
std::forward<decltype(func)>(func)(
std::forward<decltype(params)>(params)...);
stop timer and record elapsed time;
};
Run Code Online (Sandbox Code Playgroud)
我的问题是关于 std::forward<decltype(func)>(func)(...
对于在lambda表达式中使用熟悉的模板语法,这似乎是一个很好的用例,以防我们想要使计时器类型成为编译时常量.
我尝试构建一个函数模板,可以测量任意类型函数的执行时间.这是我到目前为止所尝试的:
#include <chrono>
#include <iostream>
#include <type_traits>
#include <utility>
// Executes fn with arguments args and returns the time needed
// and the result of f if it is not void
template <class Fn, class... Args>
auto timer(Fn fn, Args... args)
-> std::pair<double, decltype(fn(args...))> {
static_assert(!std::is_void<decltype(fn(args...))>::value,
"Call timer_void if return type is void!");
auto start = std::chrono::high_resolution_clock::now();
auto ret = fn(args...);
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed_seconds = end - start;
return { elapsed_seconds.count(), ret };
}
// If fn …Run Code Online (Sandbox Code Playgroud)