本文介绍了这是溢出吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有这段代码:struct timeval start, end;gettimeofday(&start, NULL);//code I'm timinggettimeofday(&end, NULL);long elapsed = ((end.tv_sec-start.tv_sec)*1000000 + end.tv_usec-start.tv_usec);ofstream timeFile;timeFile.open ("timingSheet.txt");timeFile << fixed << showpoint;timeFile << setprecision(2);timeFile << "Duration: " << elapsed << "\n";timeFile.close();这将输出已经超过的微秒数。但是,如果我改变这一行Which will output the number of microseconds that has passed. However, if I change this linelong elapsed = ((end.tv_sec-start.tv_sec)*1000000 + end.tv_usec-start.tv_usec);:long elapsed = ((end.tv_sec-start.tv_sec)*1000000 + end.tv_usec-start.tv_usec)/1000000.0;我得到一个负值。为什么会发生这种情况?I get a negative value. Why does this happen?推荐答案我使用一个时间类,我借用SO从这里的某处。I use a timing class that I borrowed from somewhere here on SO.#include <time.h>#include <sys/time.h>#include <iomanip>#include <iostream>using namespace std;class Timer{private:timeval startTime;public: void start() { gettimeofday(&startTime, NULL); } double stop() { timeval endTime; long seconds, useconds; double duration; gettimeofday(&endTime, NULL); seconds = endTime.tv_sec - startTime.tv_sec; useconds = endTime.tv_usec - startTime.tv_usec; duration = seconds + useconds/1000000.0; return duration; } static void printTime(double duration) { cout << setprecision(6) << fixed << duration << " seconds" << endl; }};例如:Timer timer = Timer();timer.start();long x=0;for (int i = 0; i < 256; i++) for (int j = 0; j < 256; j++) for (int k = 0; k < 256; k++) for (int l = 0; l < 256; l++) x++;timer.printTime(timer.stop());产生 11.346621秒。对于我的散列函数项目,我得到:For my hash function project, I get:Number of collisions: 0Set size: 16777216VM: 841.797MB22.5810500000 seconds 这篇关于这是溢出吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-21 20:41