我想比较使用Python和C++从stdin读取字符串的读取行,并且看到我的C++代码运行速度比等效的Python代码慢一个数量级,这让我很震惊.由于我的C++生锈了,我还不是专家Pythonista,请告诉我,如果我做错了什么或者我是否误解了什么.
(TLDR回答:包括声明:cin.sync_with_stdio(false)或者只是fgets改用.
TLDR结果:一直向下滚动到我的问题的底部并查看表格.)
C++代码:
#include <iostream>
#include <time.h>
using namespace std;
int main() {
string input_line;
long line_count = 0;
time_t start = time(NULL);
int sec;
int lps;
while (cin) {
getline(cin, input_line);
if (!cin.eof())
line_count++;
};
sec = (int) time(NULL) - start;
cerr << "Read " << line_count << " lines in " << sec << " seconds.";
if (sec > 0) {
lps = line_count / sec;
cerr << " LPS: " << lps …Run Code Online (Sandbox Code Playgroud) 如何在C中获得微秒时间戳?
我正在尝试:
struct timeval tv;
gettimeofday(&tv,NULL);
return tv.tv_usec;
Run Code Online (Sandbox Code Playgroud)
但是这会返回一些无意义的值,如果我得到两个时间戳,第二个可以比第一个更小或更大(第二个应该总是更大).是否可以将gettimeofday返回的魔术整数转换为可以实际使用的正常数字?
如已知clock()可能显示小于或大于实时值 - 两种情况都显示在以下示例1和2中.
为了在C++ 11中高精度测量时间,我们可以使用:
std::chrono::high_resolution_clock::now(); - 保证高精度std::chrono::steady_clock::now(); - 保证实时测量clock(); - 保证高精度,但测量CPU周期而不是时间time(&t_start); - 不是高精度,而是实时测量1-例如:http://ideone.com/SudWTM
#include <stdio.h>
#include <time.h>
#include <thread>
#include <iostream>
#include <chrono>
int main(void) {
std::cout << "sleep(3) took: \n\n";
clock_t c_start, c_end;
time_t t_start, t_end;
std::chrono::high_resolution_clock::time_point h_start, h_end;
std::chrono::steady_clock::time_point steady_start, steady_end;
time(&t_start); // less precise than clock() but always get the real actual time
c_start = clock(); // clock() get only CPU-time, it can be more than …Run Code Online (Sandbox Code Playgroud) 我终于弄明白这一点,想分享知识并节省一些时间,所以请看下面的答案.但是,我仍然需要Linux的答案,所以如果你知道,请回答,因为我的答案中的代码仅适用于Windows.
更新:我也想到了Linux,包括前Python 3.3(例如:Raspberry Pi),我在下面的答案中发布了我的新模块/代码.
我原来的问题:如何在Python中获得毫秒和微秒分辨率的时间戳?我也喜欢类似Arduino的延迟和delayMicroseconds()函数.
几个月前,这个问题被标记为这个问题的副本.看这里:
它说,"这个问题在这里已有答案." 不幸的是,这不是真的.几年前我在问这个问题之前就读过这些答案了,他们没有回答我的问题,也没有满足我的需要.它们和我的问题一样不适用,因为它是最黑的回答,因为它依赖于time模块,因为它依赖于模块,因为它不具有任何类型的保证分辨率,因此它是不可思议的错误:
请重新打开我的问题.它不是重复的.它没有其他问题的事先答案.链接已经包含答案的问题依赖于time模块,甚至表明它的解决方案已经到处都是.最热门的答案是使用16毫秒的答案引用Windows分辨率,这比我在此提供的答案(0.5 美分分辨率)差32000倍.同样,我需要1 ms和1 us(或类似)分辨率,而不是16000 us分辨率.因此,它不是重复的.
谢谢你的时间.:)
我想使用std::chrono::duration文字,例如10s表示“10 秒”,如下所示:
std::chrono::duration<uint64_t, std::milli> millisecs = 10s;
Run Code Online (Sandbox Code Playgroud)
但是,我收到此错误:
Run Code Online (Sandbox Code Playgroud)main.cpp:20:17: error: unable to find numeric literal operator 'operator""s' millisecs = 20s; main.cpp:22:17: note: use -std=gnu++11 or -fext-numeric-literals to enable more built-in suffixes
我已经添加-fext-numeric-literals到我的 gcc 编译命令中:
std::chrono::duration<uint64_t, std::milli> millisecs = 10s;
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
如果您不熟悉 Gaffer on Games 文章“修复您的时间步长”,您可以在这里找到它:https : //gafferongames.com/post/fix_your_timestep/
我正在构建一个游戏引擎,为了让 std::chrono 更加舒适,我一直在尝试使用 std::chrono 实现一个固定的时间步长......几天了,但我不能似乎把我的头环绕在它周围。这是我正在努力的伪代码:
double t = 0.0;
double dt = 0.01;
double currentTime = hires_time_in_seconds();
double accumulator = 0.0;
State previous;
State current;
while ( !quit )
{
double newTime = time();
double frameTime = newTime - currentTime;
if ( frameTime > 0.25 )
frameTime = 0.25;
currentTime = newTime;
accumulator += frameTime;
while ( accumulator >= dt )
{
previousState = currentState;
integrate( currentState, t, dt );
t += …Run Code Online (Sandbox Code Playgroud) 事实上,我正在尝试计算函数在我的程序中完成所需的时间.因此,我在调用函数时使用逻辑来获取系统时间,并在函数返回值时使用时间,然后减去我得到完成时间的值.因此,如果有人能告诉我一些更好的方法,或者只是如何在实例中获得系统时间,那将是非常有帮助的
我正在评估我的项目的网络+渲染工作负载。
程序连续运行一个主循环:
while (true) {
doSomething()
drawSomething()
doSomething2()
sendSomething()
}
Run Code Online (Sandbox Code Playgroud)
主循环每秒运行 60 多次。
我想查看性能故障,每个程序需要多少时间。
我担心的是,如果我打印每个程序的每个入口和出口的时间间隔,
这会导致巨大的性能开销。
我很好奇什么是衡量性能的惯用方法。
日志打印是否足够好?
c++ ×5
benchmarking ×2
c++-chrono ×2
c++11 ×2
python ×2
c ×1
delay ×1
game-engine ×1
game-loop ×1
gcc ×1
getline ×1
iostream ×1
milliseconds ×1
multiline ×1
performance ×1
string ×1
time ×1
timestamp ×1
timing ×1