标签: ctime

精确的时间测量

我在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++ visual-studio-2010 ctime c++-chrono

45
推荐指数
3
解决办法
7万
查看次数

随机布尔值

我正在尝试生成一个在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)

c++ random ctime

24
推荐指数
2
解决办法
3万
查看次数

fprintf和ctime没有从ctime传递\n

我在文本文件中插入时间有问题.我使用下面的代码,我得到|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)

c printf file ctime text-files

15
推荐指数
5
解决办法
2万
查看次数

如何修复MacOSX上全局命名空间错误中没有成员的丢失时间?

我正在尝试在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)

c++ macos ctime clang++

11
推荐指数
2
解决办法
6204
查看次数

对tm结构添加一些间隔

我有一个结构TM.
我需要在tm结构中添加一些固定的间隔(在xx年,xx个月,xx天给出).
这有什么标准功能吗?

我使用的编译器是Windows XP上的MSVC 2005.

c time-t ctime

10
推荐指数
2
解决办法
9913
查看次数

<chrono>和<ctime>有什么区别?

为了测量函数的执行时间,我可以同时使用它们.但使用<chrono>和有<ctime>什么区别?我应该更喜欢一个而不是另一个吗?

c++ ctime c++11 c++-chrono

10
推荐指数
2
解决办法
4106
查看次数

如何在Mac OS上使用ruby设置文件创建时间?

我正在尝试使用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)

ruby filesystems macos file ctime

9
推荐指数
1
解决办法
4458
查看次数

ctime,mtime,持有目录,windows,linux

让我们一劳永逸地澄清一下.我尝试谷歌这个,但似乎在一个地方找不到这些信息.

创建或删除文件时,保留目录mtime在Windows和Linux上都会更改.ctime在Linux机器人上也没有在Windows上发生变化,因为ctime是创建时间.

如果重新打开并写入文件,则保留目录不会更改.但是,在Windows和Linux上,文件mtime都会发生变化,而在Linux上,ctime也会发生变化,在Windows上,ctime是创建时间.

它是否正确?有什么警告吗?比如Windows网络共享有例外吗?还是桑巴?

编辑:那些投票决定关闭此主题的人,请在主题上留下关于您认为哪个网站的评论.Stackoverflow上有大量的mtime/ctime问题,仅仅因为我没有包含依赖于这些知识的PHP片段,这并不意味着没有:/

unix windows filesystems ctime filemtime

9
推荐指数
1
解决办法
2339
查看次数

如何在Unix中修改文件的"上次状态更改"(ctime)属性?

我知道有一种方法可以通过使用" touch "命令修改Unix系统中给定文件的" 修改 "(mtime)和" 上次访问 "(atime)时间属性.但我想知道是否存在修改"上次状态更改"(ctime)属性的方法?

unix ctime

8
推荐指数
1
解决办法
8537
查看次数

为什么在 C++ 中获取日期和/或时间如此复杂?

我完全希望这会在一两天内关闭,因为这是一个主观的话题,但无论如何都要说:为什么在 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)

c++ ctime localtime c++-chrono

8
推荐指数
1
解决办法
731
查看次数