我需要在Linux(以及其他类Unix系统)上的C++中测量长计算所花费的CPU(而非挂钟)时间,所以我使用的是clock().问题:在32位系统上,大约2000或4000秒后会出现这种情况.
推荐的解决方法是什么?
我有一个依赖时钟时间来执行一些重复任务的程序。我怎么知道时钟已经改变,我需要重新调整我的任务?
编辑:我正在使用一个Date实例来查看时间是否已经过去。
有兴趣通过自己的模拟时钟应用程序创建 - 任何人都知道一些实现模拟时钟的示例项目/代码?(即不是数字时钟).理想情况下,自定义背景和自定义时针/分针作为图像?
我注意到存在以下链接,但是希望有一些更具体的东西来匹配我正在做的事情(背景图像,时针/分针的图像): 示例代码
这是我的代码:
// Start performance test clock
assert((start=clock())!=-1);
// Some reading and writing methods
// Get stop time
stop = clock();
cout << stop << endl;
// Calculate operation time
double result = (double)(stop-start)/CLOCKS_PER_SEC;
// Print result
cout << "--> Finished analysing in " << result << "s" << endl;
Run Code Online (Sandbox Code Playgroud)
我调试程序时效果很好,但是当我运行发布版本时,stop会收到比start更小的值,结果是负数.
有任何想法吗?
我试着用C++测量我的代码执行情况.但那不是很准确.这是我的代码:
std::clock_t start2;
start2 = std::clock();
myFunction();
printf("time of execution: %d ms\r\n",std::clock()-start2);
Run Code Online (Sandbox Code Playgroud)
但是不准确足够的执行时间低于10毫秒,但结果是10毫秒或0毫秒,这是不稳定的.
所以我在寻找更准确的解决方案.有什么建议吗?
我想在python shell中编写简单的数字时钟.我想尽可能避免使用tkinter.这就是我现在拥有的;
import time
while True:
from datetime import datetime
now = datetime.now()
print ("%s/%s/%s %s:%s:%s" % (now.month,now.day,now.year,now.hour,now.minute,now.second))
time.sleep(1)
Run Code Online (Sandbox Code Playgroud)
这会产生反复出现的打印,就像这样;
06/29/16 23:08:32
06/29/16 23:08:33
06/29/16 23:08:34
Run Code Online (Sandbox Code Playgroud)
我知道这很粗糙,我还在学习.我只想要在shell中使用"滴答"数字时钟的一行.我在空闲和Windows 10上使用python 3.5.1.
如果这是不可能的,我非常想知道原因.
非常感谢
我在这个论坛上得到了一些关于如何在 Python 2 中编写时钟对象的很好的提示。我现在有一些代码可以工作。这是一个以 60 FPS“滴答”的时钟:
import sys
import time
class Clock(object):
def __init__(self):
self.init_os()
self.fps = 60.0
self._tick = 1.0 / self.fps
print "TICK", self._tick
self.check_min_sleep()
self.t = self.timestamp()
def init_os(self):
if sys.platform == "win32":
self.timestamp = time.clock
self.wait = time.sleep
def timeit(self, f, args):
t1 = self.timestamp()
f(*args)
t2 = self.timestamp()
return t2 - t1
def check_min_sleep(self):
"""checks the min sleep time on the system"""
runs = 1000
times = [self.timeit(self.wait, (0.001, )) for n in xrange(runs)]
average …Run Code Online (Sandbox Code Playgroud) #include <iostream>
#include <chrono>
#include <time.h>
#include <stdio.h>
using namespace std;
using namesapce chrono;
int main() {
int f;
time_t start, end;
time (&start);
cin >> f;
time (&end);
double dif = difftime (end, start);
printf ("Elapsed time is %.2lf seconds.", dif );
}
Run Code Online (Sandbox Code Playgroud)
大家好,我目前正在进行C++任务,基本上我需要在10秒内让用户输入内容.我设法找出如何计算秒的时间,但我需要它是毫秒,因为我必须找出10秒以上的毫秒数.我不熟悉C++,并且非常感谢任何可能有助于引导我朝着正确方向前进的建议.非常感谢
我试图在java中实现一个简单的模拟时钟.我的时钟成功显示了我第一次运行程序的时间,但是在打开时,时间保持不变,没有任何绘图更新并冻结.我不明白为什么它不工作,我已经通过论坛试图寻找有类似问题的人,但不能.如果有人能找到我错的地方,我将非常感谢,谢谢
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing. JPanel;
public class ClockFace extends JPanel {
Date date = new Date();
private BufferedImage clockFace;
public ClockFace() {
this.init();
this.startClock();
}
private void init() {
JFrame window = new JFrame("Clock");
window.setContentPane(this);
this.setPreferredSize(new Dimension(600,600));
window.pack();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
window.setLocationRelativeTo(null);
}
//Draws the image to the ClockFace panel
public void paintComponent(Graphics g) {
Graphics2D twoD = (Graphics2D) g;
twoD.drawImage(clockFace, 0, 0, null);
}
public …Run Code Online (Sandbox Code Playgroud) 我正在C中实现一个数学库,该数学库大量使用乘法。最初,我所有的乘法都是使用进行的uint16_t。最近,我将其中的许多更改为,uint32_t然后发现我的代码运行时间几乎翻了一番。正如我在Intel x64处理器中所认为的那样,我感到困惑,32位和16位乘法需要相同的时钟周期。我写了诊断代码,请在下面找到
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <time.h>
#include "cpucycles.c"
#define REPEAT 10000
#define OUT_REPEAT 100000
void main(){
uint16_t a_16[REPEAT], b_16[REPEAT], c_16[REPEAT];
uint32_t a_32[REPEAT], b_32[REPEAT], c_32[REPEAT];
int32_t i,j;
uint64_t clock1, clock2, CLOCK16, CLOCK32;
uint64_t acc=0;
time_t t;
srand((unsigned) time(&t));
clock1=clock2=CLOCK16=CLOCK32=0;
for(j=0;j<OUT_REPEAT;j++){
for(i=0;i<REPEAT;i++){
a_16[i]=rand()& ( (1<<13) -1); //need 13-bit integers only
b_16[i]=rand()& ( (1<<13) -1);
a_32[i]=rand()&( (1<<19) -1);
b_32[i]=rand()&( (1<<19) -1); //need 19-bit integers only
}
clock1=cpucycles();
for(i=0;i<REPEAT;i++){
c_16[i]=a_16[i]*b_16[i];
}
clock2=cpucycles();
CLOCK16=CLOCK16+(clock2-clock1);
clock1=cpucycles();
for(i=0;i<REPEAT;i++){
c_32[i]=a_32[i]*b_32[i]; …Run Code Online (Sandbox Code Playgroud)