Created
March 19, 2022 11:28
-
-
Save kissge/8d4fb3a8f0de5f8850aa031e188f81e8 to your computer and use it in GitHub Desktop.
date(1) 速すぎない?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// g++ -std=c++11 -O3 date-iso.cpp -o date-iso | |
#include <chrono> | |
#include <cstdio> | |
using namespace std::chrono; | |
int main() | |
{ | |
auto now = system_clock::now(); | |
auto timet = system_clock::to_time_t(now); | |
std::tm tm{}; | |
localtime_r(&timet, &tm); | |
char buffer[27] = {0}; | |
std::strftime(buffer, 27, "%FT%T.%z\n", &tm); | |
fwrite(buffer, 1, 20, stdout); | |
auto nanosec = duration_cast<nanoseconds>(now.time_since_epoch()).count() % 1000000000LL; | |
printf("%09lld", nanosec); | |
fwrite(buffer + 20, 1, 6, stdout); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ uname -a | |
Linux xxxxxxxx 5.12.8-arch1-1 #1 SMP PREEMPT Fri, 28 May 2021 15:10:20 +0000 x86_64 GNU/Linux | |
$ g++ -v | |
Using built-in specs. | |
COLLECT_GCC=g++ | |
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.0/lto-wrapper | |
Target: x86_64-pc-linux-gnu | |
Configured with: /build/gcc/src/gcc/configure --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++,d --enable-bootstrap --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --with-linker-hash-style=gnu --with-system-zlib --enable-__cxa_atexit --enable-cet=auto --enable-checking=release --enable-clocale=gnu --enable-default-pie --enable-default-ssp --enable-gnu-indirect-function --enable-gnu-unique-object --enable-linker-build-id --enable-lto --enable-multilib --enable-plugin --enable-shared --enable-threads=posix --disable-libssp --disable-libstdcxx-pch --disable-werror --with-build-config=bootstrap-lto --enable-link-serialization=1 gdc_include_dir=/usr/include/dlang/gdc | |
Thread model: posix | |
Supported LTO compression algorithms: zlib zstd | |
gcc version 11.2.0 (GCC) | |
$ g++ -std=c++11 -O3 date-iso.cpp -o date-iso | |
$ time (repeat 10000 { ./date-iso > /dev/null }) | |
( repeat 10000; do; ./date-iso > /dev/null; done; ) 9.20s user 2.45s system 98% cpu 11.776 total | |
$ time (repeat 10000 { date +%Y-%m-%dT%H:%M:%S.%N%z > /dev/null }) | |
( repeat 10000; do; date +%Y-%m-%dT%H:%M:%S.%N%z > /dev/null; done; ) 4.95s user 1.52s system 98% cpu 6.562 total |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ uname -a | |
Darwin xxxxxxxx 20.5.0 Darwin Kernel Version 20.5.0: Sat May 8 05:10:33 PDT 2021; root:xnu-7195.121.3~9/RELEASE_X86_64 x86_64 | |
$ g++ -v | |
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1 | |
Apple clang version 12.0.5 (clang-1205.0.22.11) | |
Target: x86_64-apple-darwin20.5.0 | |
Thread model: posix | |
InstalledDir: /Library/Developer/CommandLineTools/usr/bin | |
$ g++ -std=c++11 -O3 date-iso.cpp -o date-iso | |
$ time (repeat 10000 { ./date-iso > /dev/null }) | |
( repeat 10000; do; ./date-iso > /dev/null; done; ) 7.38s user 9.74s system 70% cpu 24.201 total | |
$ time (repeat 10000 { gdate +%Y-%m-%dT%H:%M:%S.%N%z > /dev/null }) | |
( repeat 10000; do; gdate +%Y-%m-%dT%H:%M:%S.%N%z > /dev/null; done; ) 8.76s user 11.61s system 69% cpu 29.247 total | |
$ # 参考記録(macOSのdateは秒の小数点以下部分を表示できない) | |
$ time (repeat 10000 { date +%Y-%m-%dT%H:%M:%S%z > /dev/null }) | |
( repeat 10000; do; date +%Y-%m-%dT%H:%M:%S%z > /dev/null; done; ) 5.02s user 9.73s system 82% cpu 17.784 total | |
$ ./date-iso | |
2022-03-19T20:20:39.479847000+0900 | |
$ gdate +%Y-%m-%dT%H:%M:%S.%N%z | |
2022-03-19T20:20:42.207666000+0900 | |
$ date +%Y-%m-%dT%H:%M:%S%z | |
2022-03-19T20:20:46+0900 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment