我在C++中使用time.h来测量函数的时间.
clock_t t = clock();
someFunction();
printf("\nTime taken: %.4fs\n", (float)(clock() - t)/CLOCKS_PER_SEC);
Run Code Online (Sandbox Code Playgroud)
但是,我总是把时间花在0.0000上.clock()和t单独打印时,具有相同的值.我想知道是否有办法在C++中精确测量时间(可能是纳秒级).我正在使用VS2010.
我正在尝试生成一个在C++中为0或1的随机int.现在,每次运行此代码时都会收到0,我不知道为什么.这有什么问题?
#include <ctime>
#include <cstdlib>
srand(time(0));
int randomval = rand() % 2;
cout << randomval << endl;
Run Code Online (Sandbox Code Playgroud) 我在文本文件中插入时间有问题.我使用下面的代码,我得到|21,43,1,3,10,5| Wed Feb 01 20:42:32 2012哪些是正常的,但我想要做的是把数字之前的时间放在例如像,Wed Feb 01 20:42:32 2012 |21,43,1,3,10,5|但是,我不能这样做因为当我使用fprintf与ctimef函数之前fprintf它识别的数字\n在ctime内,因此它更改第1行然后打印数字.它像:
Wed Feb 01 20:42:32 2012
|21,43,1,3,10,5|
Run Code Online (Sandbox Code Playgroud)
这是我不想要的东西......我如何能够在没有切换到文本中的下一行的情况下缩短时间?提前致谢!
fprintf(file," |");
for (i=0;i<6;i++)
{
buffer[i]=(lucky_number=rand()%49+1); //range 1-49
for (j=0;j<i;j++)
{
if (buffer[j]==lucky_number)
i--;
}
itoa (buffer[i],draw_No,10);
fprintf(file,"%s",draw_No);
if (i!=5)
fprintf(file,",");
}
fprintf(file,"| %s",ctime(&t));
Run Code Online (Sandbox Code Playgroud) 我正在尝试在Maverick 10.9上的命令行上编译一个项目.该项目在Linux上完美编译.显然,MacOSX上的ctime似乎存在问题.错误是
$ make
Compiling src//core/AbstractARAClient.cpp
In file included from src//core/AbstractARAClient.cpp:5:
In file included from include/AbstractARAClient.h:8:
In file included from include/ARAMacros.h:14:
In file included from include/Address.h:9:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/sstream:174:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/ostream:131:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/ios:216:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__locale:18:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/mutex:176:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__mutex_base:15:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/chrono:279:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/ctime:56:9: error: no member named
'clock_t' in the global namespace
using ::clock_t;
~~^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/ctime:58:9: error: no member named
'time_t' …Run Code Online (Sandbox Code Playgroud) 我有一个结构TM.
我需要在tm结构中添加一些固定的间隔(在xx年,xx个月,xx天给出).
这有什么标准功能吗?
我使用的编译器是Windows XP上的MSVC 2005.
为了测量函数的执行时间,我可以同时使用它们.但使用<chrono>和有<ctime>什么区别?我应该更喜欢一个而不是另一个吗?
我正在尝试使用ruby脚本在Mac OS上设置文件的文件系统创建时间.
在Mac OS X上,'ctime'表示最后一次修改inode而不是文件创建时间,因此使用ruby的File.utime()来设置ctime将无济于事.
使用此提示[ http://inessential.com/2008/12/18/file_creation_date_in_ruby_on_macs ]我可以检索文件的创建时间:
Time.parse(`mdls -name kMDItemContentCreationDate -raw "#{filename}"`)
Run Code Online (Sandbox Code Playgroud)
...但是有关如何使用ruby设置它的任何想法?
- 更新 -
好吧,我想我能实际上做到这一点File.utime的红宝石.
尽管Mac OS在技术上并未使用ctime来跟踪文件创建时间,但当您使用utime更新ctime(以及必须同时设置的mtime)时,文件系统似乎神奇地也会更新创建时间kMDItemContentCreationDate.
因此,要将文件名设置为2010年10月1日的ctime和2010年10月2日的mtime:
File.utime(Time.strptime('011010', '%d%m%y'), Time.strptime('021010', '%d%m%y'), filename)
Run Code Online (Sandbox Code Playgroud) 让我们一劳永逸地澄清一下.我尝试谷歌这个,但似乎在一个地方找不到这些信息.
创建或删除文件时,保留目录mtime在Windows和Linux上都会更改.ctime在Linux机器人上也没有在Windows上发生变化,因为ctime是创建时间.
如果重新打开并写入文件,则保留目录不会更改.但是,在Windows和Linux上,文件mtime都会发生变化,而在Linux上,ctime也会发生变化,在Windows上,ctime是创建时间.
它是否正确?有什么警告吗?比如Windows网络共享有例外吗?还是桑巴?
编辑:那些投票决定关闭此主题的人,请在主题上留下关于您认为哪个网站的评论.Stackoverflow上有大量的mtime/ctime问题,仅仅因为我没有包含依赖于这些知识的PHP片段,这并不意味着没有:/
我知道有一种方法可以通过使用" touch "命令修改Unix系统中给定文件的" 修改 "(mtime)和" 上次访问 "(atime)时间属性.但我想知道是否存在修改"上次状态更改"(ctime)属性的方法?
我完全希望这会在一两天内关闭,因为这是一个主观的话题,但无论如何都要说:为什么在 C++ 中获取日期/时间至少需要 5 行代码?
这是我在 C 中学会的第一件事,但那是很久以前的事了……我记得当时我花了一段时间才掌握整个概念。现在我更有经验了,但是在习惯了 C# 和 Java 等高级语言之后,我真的很恼火,这么简单的事情需要所有这些:
#include <iostream>
#include <chrono>
#include <ctime>
using namespace std::chrono;
// First get an epoch value
auto time = system_clock::to_time_t(system_clock::now());
// Now we need a char buffer to hold the output
char timeString[20] = "";
// Oh and a tm struct too! Gotta have that, just to make it more complicated!
tm tmStruct;
// Oh and BTW, you can't just get your local time directly;
// you …Run Code Online (Sandbox Code Playgroud)