我有一个 UTC 日期时间,没有存储在 uint64 中的格式,即:20090520145024798
我需要得到这个时间的小时、分钟、秒和毫秒。通过将其转换为字符串并使用子字符串,我可以很容易地做到这一点。但是,这段代码需要非常快,所以我想避免字符串操作。有没有更快的方法,也许使用位操作来做到这一点?哦,顺便说一句,这需要在 Linux 上的 C++ 中完成。
问问题
12426 次
2 回答
8
uint64 u = 20090520145024798;
unsigned long w = u % 1000000000;
unsigned millisec = w % 1000;
w /= 1000;
unsigned sec = w % 100;
w /= 100;
unsigned min = w % 100;
unsigned hour = w / 100;
unsigned long v = w / 1000000000;
unsigned day = v % 100;
v /= 100;
unsigned month = v % 100;
unsigned year = v / 100;
该解决方案从中间切换uint64 u
到unsigned long w
(和v
)的原因是 YYYYMMDD 和 HHMMSSIII 适合 32 位,并且在某些系统上 32 位除法比 64 位除法更快。
于 2009-05-21T00:17:53.573 回答
6
为了建立在 pts 和 onebyone 的建议的基础上,这里是他们在核心 2 处理器上使用 32 位和 64 位操作的方法的基准:
#include <stdio.h>
#include <sys/time.h>
#include <sys/resource.h>
typedef unsigned long long uint64;
struct outs {
unsigned millisec, sec, min, hour, day, month, year;
};
void tbreakdown2(uint64 u, struct outs *outp) {
outp->millisec = u % 1000;
u /= 1000;
outp->sec = u % 100;
u /= 100;
outp->min = u % 100;
u /= 100;
outp->hour = u % 100;
unsigned long v = u / 100;
outp->day = v % 100;
v /= 100;
outp->month = v % 100;
outp->year = v / 100;
}
void tbreakdown(uint64 u, struct outs *outp) {
unsigned int daypart, timepart; //4000000000
// YYYYMMDD
//HHMMssssss
daypart = u / 1000000000ULL;
timepart = u % 1000000000ULL;
outp->millisec = timepart % 1000;
timepart /= 1000;
outp->sec = timepart % 100;
timepart /= 100;
outp->min = timepart % 100;
timepart /= 100;
outp->hour = timepart;
outp->day = daypart % 100;
daypart /= 100;
outp->month = daypart % 100;
daypart /= 100;
outp->year = daypart;
}
uint64 inval = 20090520145024798ULL;
void printstruct(uint64 u, struct outs *outp) {
printf("%018llu\n", u);
printf("%04d-%02d-%02d %02d:%02d:%02d.%04d\n",
outp->year, outp->month, outp->day,
outp->hour, outp->min, outp->sec,
outp->millisec);
}
void print_elapsed(struct timeval *tv_begin, struct timeval *tv_end) {
unsigned long long mcs_begin, mcs_end, mcs_delta;
mcs_begin = (unsigned long long)tv_begin->tv_sec * 1000000ULL;
mcs_begin += tv_begin->tv_usec;
mcs_end = (unsigned long long)tv_end->tv_sec * 1000000ULL;
mcs_end += tv_end->tv_usec;
mcs_delta = mcs_end - mcs_begin;
printf("Elapsed time: %llu.%llu\n", mcs_delta / 1000000ULL, mcs_delta % 1000000ULL);
}
int main() {
struct outs out;
struct outs *outp = &out;
struct rusage rusage_s;
struct rusage begin, end;
__sync_synchronize();
printf("Testing impl 1:\n");
tbreakdown(inval, outp);
printstruct(inval, outp);
__sync_synchronize();
getrusage(RUSAGE_SELF, &begin);
for (int i = 0; i < 100000000; i++) {
__sync_synchronize();
tbreakdown(inval, outp);
__sync_synchronize();
}
getrusage(RUSAGE_SELF, &end);
print_elapsed(&begin.ru_utime, &end.ru_utime);
printf("Testing impl 2:\n");
tbreakdown2(inval, outp);
printstruct(inval, outp);
__sync_synchronize();
getrusage(RUSAGE_SELF, &begin);
for (int i = 0; i < 100000000; i++) {
__sync_synchronize();
tbreakdown2(inval, outp);
__sync_synchronize();
}
getrusage(RUSAGE_SELF, &end);
print_elapsed(&begin.ru_utime, &end.ru_utime);
return 0;
}
和输出:
=====32-bit=====
Testing impl 1:
020090520145024798
2009-05-20 14:50:24.0798
Elapsed time: 6.840427
Testing impl 2:
020090520145024798
2009-05-20 14:50:24.0798
Elapsed time: 19.921245
=====64-bit=====
Testing impl 1:
020090520145024798
2009-05-20 14:50:24.0798
Elapsed time: 3.152197
Testing impl 2:
020090520145024798
2009-05-20 14:50:24.0798
Elapsed time: 4.200262
如您所见,即使在本机 64 位模式下,避免过多的 64 位操作也会有所帮助 - 但在 32 位中会产生巨大的差异。
基准测试是在 2.2GHz 的 core2duo T7500 处理器下进行的,并在 -O3 上使用 gcc 4.3.3 编译。您看到的那些内存屏障是为了确保编译器不会尝试优化实际操作,同时允许它在选择时内联它。
于 2009-05-21T00:56:31.670 回答