我想比较使用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) 仅使用ANSI C,有没有办法以毫秒或更多精度测量时间?我正在浏览time.h但我只发现了第二个精确功能.
有没有办法在Python中以高精度测量时间---比一秒更精确?我怀疑是否有跨平台的方式来做到这一点; 我对Unix上的高精度时间感兴趣,特别是在Sun SPARC机器上运行的Solaris.
timeit似乎能够进行高精度的时间测量,但我不想测量代码片段需要多长时间,而是想直接访问时间值.
我希望在用C++实现的程序的微秒内获得准确的执行时间.我试图用clock_t获得执行时间,但这不准确.
gcc (GCC) 4.6.3
c89
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用usleep.但是,我一直收到以下警告:
隐式声明函数usleep
我已经包含了unistd.h头文件.
手册页提到了这个问题.但我不确定我理解它:
usleep():
Since glibc 2.12:
_BSD_SOURCE ||
(_XOPEN_SOURCE >= 500 ||
_XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED) &&
!(_POSIX_C_SOURCE >= 200809L || _XOPEN_SOURCE >= 700)
Before glibc 2.12:
_BSD_SOURCE || _XOPEN_SOURCE >= 500 || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
Run Code Online (Sandbox Code Playgroud)
但不确定我对上述内容做了什么?
请考虑以下代码:
#include <stdio.h>
#include <time.h>
#include <math.h>
// Compile with gcc -lrt -lm -o test_clock test_clock.c
#define CLOCK CLOCK_MONOTONIC
int main(int argc, char** argv) {
double temp, elapsed;
int j;
struct timespec requestStart, requestEnd, req;
// Pseudo-sleep
clock_gettime(CLOCK, &requestStart);
temp = 0;
for(j=0; j < 40; j++)
temp += sin(j);
clock_gettime(CLOCK, &requestEnd);
elapsed = ( requestEnd.tv_sec - requestStart.tv_sec ) / 1e-6
+ ( requestEnd.tv_nsec - requestStart.tv_nsec ) / 1e3;
printf("Elapsed: %lf us\n", elapsed);
// Nanosleep
clock_gettime(CLOCK, &requestStart);
req.tv_nsec = 5000; …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分辨率.因此,它不是重复的.
谢谢你的时间.:)
我已经阅读了这个问题,但我想获得比一秒更好的计时精度:这可以通过 libc 的某些功能实现吗?
这是要使用 inside with nogil,所以当然不允许使用 Python ...
我想使用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)
我究竟做错了什么?
我必须在多个线程期间消耗测量时间。我必须得到这样的输出:
Starting Time | Thread Number
00000000000 | 1
00000000100 | 2
00000000200 | 3
Run Code Online (Sandbox Code Playgroud)
首先,我使用了 gettimeofday 但我看到有一些负数,然后我做了一些研究并了解到 gettimeofday 无法可靠地测量经过的时间。然后我决定使用clock_gettime(CLOCK_MONOTONIC)。
然而,有一个问题。当我用秒来测量时间时,我无法精确地测量时间。当我使用纳秒时, end.tv_nsec 变量的长度不能超过 9 位数字(因为它是一个长变量)。这意味着,当它必须移动到第 10 位时,它仍然保持在 9 位,并且实际上数字变小,导致经过的时间为负数。
这是我的代码:
long elapsedTime;
struct timespec end;
struct timespec start2;
//gettimeofday(&start2, NULL);
clock_gettime(CLOCK_MONOTONIC,&start2);
while(c <= totalCount)
{
if(strcmp(algorithm,"FCFS") == 0)
{
printf("In SErunner count=%d \n",count);
if(count > 0)
{
printf("Count = %d \n",count);
it = deQueue();
c++;
tid = it->tid;
clock_gettime(CLOCK_MONOTONIC,&end);
usleep( 1000*(it->value));
elapsedTime = ( end.tv_sec - start2.tv_sec);
printf("Process of thread …Run Code Online (Sandbox Code Playgroud) c ×6
python ×4
time ×3
c++ ×2
linux ×2
benchmarking ×1
c++-chrono ×1
clock ×1
cython ×1
delay ×1
gcc ×1
getline ×1
glibc ×1
iostream ×1
libc ×1
milliseconds ×1
performance ×1
portability ×1
pthreads ×1
real-time ×1
timestamp ×1
timing ×1
unix ×1
usleep ×1