#include <stdio.h>
#include <sys/time.h>
#include <time.h>
#include <stdlib.h>
static void test_localtime(int M);
static void test_localtime_r(int M);
static void test_mktime(int M);
int main(int argc, char* argv[])
{
const int M = (argc<2)? 1000000: atoi(argv[1]);
test_localtime(M);
printf("\n");
test_localtime_r(M);
printf("\n");
test_mktime(M);
return 0;
}
void test_localtime(int M)
{
int i;
time_t now = time(NULL);
struct timeval tv1, tv2;
printf("test: localtime ...\n");
unsetenv("TZ");
{
struct tm* result1;
gettimeofday(&tv1, NULL);
for (i=0; i<M; ++i)
{
result1 = localtime(&now);
}
gettimeofday(&tv2, NULL);
printf("TZ is NULL: %ums\n", (tv2.tv_sec-tv1.tv_sec)*1000 + (tv2.tv_usec-tv1.tv_usec)/1000);
}
setenv("TZ", "", 0);
{
struct tm* result2;
gettimeofday(&tv1, NULL);
for (i=0; i<M; ++i)
{
result2 = localtime(&now);
}
gettimeofday(&tv2, NULL);
printf("TZ is empty: %ums\n", (tv2.tv_sec-tv1.tv_sec)*1000 + (tv2.tv_usec-tv1.tv_usec)/1000);
}
setenv("TZ", "Asia/Shanghai", 0);
{
struct tm* result3;
gettimeofday(&tv1, NULL);
for (i=0; i<M; ++i)
{
result3 = localtime(&now);
}
gettimeofday(&tv2, NULL);
printf("TZ is Asia/Shanghai: %ums\n", (tv2.tv_sec-tv1.tv_sec)*1000 + (tv2.tv_usec-tv1.tv_usec)/1000);
}
}
void test_localtime_r(int M)
{
int i;
time_t now = time(NULL);
struct timeval tv1, tv2;
printf("test: localtime_r ...\n");
unsetenv("TZ");
{
struct tm result1;
result1.tm_isdst = 1;
gettimeofday(&tv1, NULL);
for (i=0; i<M; ++i)
{
localtime_r(&now, &result1);
}
gettimeofday(&tv2, NULL);
printf("TZ is NULL and isdst=1: %ums\n", (tv2.tv_sec-tv1.tv_sec)*1000 + (tv2.tv_usec-tv1.tv_usec)/1000);
}
{
struct tm result2;
result2.tm_isdst = 0;
gettimeofday(&tv1, NULL);
for (i=0; i<M; ++i)
{
localtime_r(&now, &result2);
}
gettimeofday(&tv2, NULL);
printf("TZ is NULL and isdst=0: %ums\n", (tv2.tv_sec-tv1.tv_sec)*1000 + (tv2.tv_usec-tv1.tv_usec)/1000);
}
{
struct tm result3;
result3.tm_isdst = -1;
gettimeofday(&tv1, NULL);
for (i=0; i<M; ++i)
{
localtime_r(&now, &result3);
}
gettimeofday(&tv2, NULL);
printf("TZ is NULL and isdst=-1: %ums\n", (tv2.tv_sec-tv1.tv_sec)*1000 + (tv2.tv_usec-tv1.tv_usec)/1000);
}
{
struct tm result4;
gettimeofday(&tv1, NULL);
for (i=0; i<M; ++i)
{
localtime_r(&now, &result4);
}
gettimeofday(&tv2, NULL);
printf("TZ is NULL and isdst undefined: %ums\n", (tv2.tv_sec-tv1.tv_sec)*1000 + (tv2.tv_usec-tv1.tv_usec)/1000);
}
setenv("TZ", "", 0);
{
struct tm result5;
result5.tm_isdst = 1;
gettimeofday(&tv1, NULL);
for (i=0; i<M; ++i)
{
localtime_r(&now, &result5);
}
gettimeofday(&tv2, NULL);
printf("TZ is empty and isdst=1: %ums\n", (tv2.tv_sec-tv1.tv_sec)*1000 + (tv2.tv_usec-tv1.tv_usec)/1000);
}
{
struct tm result6;
result6.tm_isdst = 0;
gettimeofday(&tv1, NULL);
for (i=0; i<M; ++i)
{
localtime_r(&now, &result6);
}
gettimeofday(&tv2, NULL);
printf("TZ is empty and isdst=0: %ums\n", (tv2.tv_sec-tv1.tv_sec)*1000 + (tv2.tv_usec-tv1.tv_usec)/1000);
}
{
struct tm result7;
result7.tm_isdst = -1;
gettimeofday(&tv1, NULL);
for (i=0; i<M; ++i)
{
localtime_r(&now, &result7);
}
gettimeofday(&tv2, NULL);
printf("TZ is empty and isdst=-1: %ums\n", (tv2.tv_sec-tv1.tv_sec)*1000 + (tv2.tv_usec-tv1.tv_usec)/1000);
}
{
struct tm result8;
result8.tm_isdst = -1;
gettimeofday(&tv1, NULL);
for (i=0; i<M; ++i)
{
localtime_r(&now, &result8);
}
gettimeofday(&tv2, NULL);
printf("TZ is empty and isdst undefined: %ums\n", (tv2.tv_sec-tv1.tv_sec)*1000 + (tv2.tv_usec-tv1.tv_usec)/1000);
}
setenv("TZ", "Asia/Shanghai", 0);
{
struct tm result9;
result9.tm_isdst = 1;
gettimeofday(&tv1, NULL);
for (i=0; i<M; ++i)
{
localtime_r(&now, &result9);
}
gettimeofday(&tv2, NULL);
printf("TZ is Asia/Shanghai and isdst=1: %ums\n", (tv2.tv_sec-tv1.tv_sec)*1000 + (tv2.tv_usec-tv1.tv_usec)/1000);
}
{
struct tm result10;
result10.tm_isdst = 0;
gettimeofday(&tv1, NULL);
for (i=0; i<M; ++i)
{
localtime_r(&now, &result10);
}
gettimeofday(&tv2, NULL);
printf("TZ is Asia/Shanghai and isdst=0: %ums\n", (tv2.tv_sec-tv1.tv_sec)*1000 + (tv2.tv_usec-tv1.tv_usec)/1000);
}
{
struct tm result11;
result11.tm_isdst = -1;
gettimeofday(&tv1, NULL);
for (i=0; i<M; ++i)
{
localtime_r(&now, &result11);
}
gettimeofday(&tv2, NULL);
printf("TZ is Asia/Shanghai and isdst=-1: %ums\n", (tv2.tv_sec-tv1.tv_sec)*1000 + (tv2.tv_usec-tv1.tv_usec)/1000);
}
{
struct tm result12;
gettimeofday(&tv1, NULL);
for (i=0; i<M; ++i)
{
localtime_r(&now, &result12);
}
gettimeofday(&tv2, NULL);
printf("TZ is Asia/Shanghai and isdst undefined: %ums\n", (tv2.tv_sec-tv1.tv_sec)*1000 + (tv2.tv_usec-tv1.tv_usec)/1000);
}
}
void test_mktime(int M)
{
int i;
time_t now = time(NULL);
struct timeval tv1, tv2;
printf("test: mktime ...\n");
unsetenv("TZ");
{
struct tm result1;
localtime_r(&now, &result1);
result1.tm_isdst = 1;
gettimeofday(&tv1, NULL);
for (i=0; i<M; ++i)
{
mktime(&result1);
}
gettimeofday(&tv2, NULL);
printf("TZ is NULL and isdst=1: %ums\n", (tv2.tv_sec-tv1.tv_sec)*1000 + (tv2.tv_usec-tv1.tv_usec)/1000);
}
{
struct tm result2;
localtime_r(&now, &result2);
result2.tm_isdst = 0;
gettimeofday(&tv1, NULL);
for (i=0; i<M; ++i)
{
mktime(&result2);
}
gettimeofday(&tv2, NULL);
printf("TZ is NULL and isdst=0: %ums\n", (tv2.tv_sec-tv1.tv_sec)*1000 + (tv2.tv_usec-tv1.tv_usec)/1000);
}
{
struct tm result3;
localtime_r(&now, &result3);
result3.tm_isdst = -1;
gettimeofday(&tv1, NULL);
for (i=0; i<M; ++i)
{
mktime(&result3);
}
gettimeofday(&tv2, NULL);
printf("TZ is NULL and isdst=-1: %ums\n", (tv2.tv_sec-tv1.tv_sec)*1000 + (tv2.tv_usec-tv1.tv_usec)/1000);
}
{
struct tm result4;
localtime_r(&now, &result4);
gettimeofday(&tv1, NULL);
for (i=0; i<M; ++i)
{
mktime(&result4);
}
gettimeofday(&tv2, NULL);
printf("TZ is NULL and isdst undefined: %ums\n", (tv2.tv_sec-tv1.tv_sec)*1000 + (tv2.tv_usec-tv1.tv_usec)/1000);
}
setenv("TZ", "", 0);
{
struct tm result5;
localtime_r(&now, &result5);
result5.tm_isdst = 1;
gettimeofday(&tv1, NULL);
for (i=0; i<M; ++i)
{
mktime(&result5);
}
gettimeofday(&tv2, NULL);
printf("TZ is empty and isdst=1: %ums\n", (tv2.tv_sec-tv1.tv_sec)*1000 + (tv2.tv_usec-tv1.tv_usec)/1000);
}
{
struct tm result6;
localtime_r(&now, &result6);
result6.tm_isdst = 0;
gettimeofday(&tv1, NULL);
for (i=0; i<M; ++i)
{
mktime(&result6);
}
gettimeofday(&tv2, NULL);
printf("TZ is empty and isdst=0: %ums\n", (tv2.tv_sec-tv1.tv_sec)*1000 + (tv2.tv_usec-tv1.tv_usec)/1000);
}
{
struct tm result7;
localtime_r(&now, &result7);
result7.tm_isdst = -1;
gettimeofday(&tv1, NULL);
for (i=0; i<M; ++i)
{
mktime(&result7);
}
gettimeofday(&tv2, NULL);
printf("TZ is empty and isdst=-1: %ums\n", (tv2.tv_sec-tv1.tv_sec)*1000 + (tv2.tv_usec-tv1.tv_usec)/1000);
}
{
struct tm result8;
localtime_r(&now, &result8);
gettimeofday(&tv1, NULL);
for (i=0; i<M; ++i)
{
mktime(&result8);
}
gettimeofday(&tv2, NULL);
printf("TZ is empty and isdst undefined: %ums\n", (tv2.tv_sec-tv1.tv_sec)*1000 + (tv2.tv_usec-tv1.tv_usec)/1000);
}
setenv("TZ", "Asia/Shanghai", 0);
{
struct tm result9;
localtime_r(&now, &result9);
result9.tm_isdst = 1;
gettimeofday(&tv1, NULL);
for (i=0; i<M; ++i)
{
mktime(&result9);
}
gettimeofday(&tv2, NULL);
printf("TZ is Asia/Shanghai and isdst=1: %ums\n", (tv2.tv_sec-tv1.tv_sec)*1000 + (tv2.tv_usec-tv1.tv_usec)/1000);
}
{
struct tm result10;
localtime_r(&now, &result10);
result10.tm_isdst = 0;
gettimeofday(&tv1, NULL);
for (i=0; i<M; ++i)
{
mktime(&result10);
}
gettimeofday(&tv2, NULL);
printf("TZ is Asia/Shanghai and isdst=0: %ums\n", (tv2.tv_sec-tv1.tv_sec)*1000 + (tv2.tv_usec-tv1.tv_usec)/1000);
}
{
struct tm result11;
localtime_r(&now, &result11);
result11.tm_isdst = -1;
gettimeofday(&tv1, NULL);
for (i=0; i<M; ++i)
{
mktime(&result11);
}
gettimeofday(&tv2, NULL);
printf("TZ is Asia/Shanghai and isdst=-1: %ums\n", (tv2.tv_sec-tv1.tv_sec)*1000 + (tv2.tv_usec-tv1.tv_usec)/1000);
}
{
struct tm result12;
localtime_r(&now, &result12);
gettimeofday(&tv1, NULL);
for (i=0; i<M; ++i)
{
mktime(&result12);
}
gettimeofday(&tv2, NULL);
printf("TZ is Asia/Shanghai and isdst undefined: %ums\n", (tv2.tv_sec-tv1.tv_sec)*1000 + (tv2.tv_usec-tv1.tv_usec)/1000);
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义