#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <time.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;
gettimeofday(&tv1, NULL);
for (i=0; i<M; ++i)
{
struct tm result1_;
memcpy(&result1_, &result1, sizeof(result1_));
result1_.tm_isdst = 1;
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;
gettimeofday(&tv1, NULL);
for (i=0; i<M; ++i)
{
struct tm result2_;
memcpy(&result2_, &result2, sizeof(result2_));
result2_.tm_isdst = 0;
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;
gettimeofday(&tv1, NULL);
for (i=0; i<M; ++i)
{
struct tm result3_;
memcpy(&result3_, &result3, sizeof(result3_));
result3_.tm_isdst = -1;
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)
{
struct tm result4_;
memcpy(&result4_, &result4, sizeof(result4_));
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;
gettimeofday(&tv1, NULL);
for (i=0; i<M; ++i)
{
struct tm result5_;
memcpy(&result5_, &result5, sizeof(result5_));
result5_.tm_isdst = 1;
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;
gettimeofday(&tv1, NULL);
for (i=0; i<M; ++i)
{
struct tm result6_;
memcpy(&result6_, &result6, sizeof(result6_));
result6_.tm_isdst = 0;
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;
gettimeofday(&tv1, NULL);
for (i=0; i<M; ++i)
{
struct tm result7_;
memcpy(&result7_, &result7, sizeof(result7_));
result7_.tm_isdst = -1;
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;
gettimeofday(&tv1, NULL);
for (i=0; i<M; ++i)
{
struct tm result8_;
memcpy(&result8_, &result8, sizeof(result8_));
result8_.tm_isdst = -1;
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;
gettimeofday(&tv1, NULL);
for (i=0; i<M; ++i)
{
struct tm result9_;
memcpy(&result9_, &result9, sizeof(result9_));
result9_.tm_isdst = 1;
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;
gettimeofday(&tv1, NULL);
for (i=0; i<M; ++i)
{
struct tm result10_;
memcpy(&result10_, &result10, sizeof(result10_));
result10_.tm_isdst = 0;
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;
gettimeofday(&tv1, NULL);
for (i=0; i<M; ++i)
{
struct tm result11_;
memcpy(&result11_, &result11, sizeof(result11_));
result11_.tm_isdst = -1;
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)
{
struct tm result12_;
memcpy(&result12_, &result12, sizeof(result12_));
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);
gettimeofday(&tv1, NULL);
for (i=0; i<M; ++i)
{
struct tm result1_;
memcpy(&result1_, &result1, sizeof(result1_));
result1_.tm_isdst = 1;
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);
gettimeofday(&tv1, NULL);
for (i=0; i<M; ++i)
{
struct tm result2_;
memcpy(&result2_, &result2, sizeof(result2_));
result2_.tm_isdst = 0;
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);
gettimeofday(&tv1, NULL);
for (i=0; i<M; ++i)
{
struct tm result3_;
memcpy(&result3_, &result3, sizeof(result3_));
result3_.tm_isdst = -1;
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)
{
struct tm result4_;
memcpy(&result4_, &result4, sizeof(result4_));
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);
gettimeofday(&tv1, NULL);
for (i=0; i<M; ++i)
{
struct tm result5_;
memcpy(&result5_, &result5, sizeof(result5_));
result5_.tm_isdst = 1;
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);
gettimeofday(&tv1, NULL);
for (i=0; i<M; ++i)
{
struct tm result6_;
memcpy(&result6_, &result6, sizeof(result6_));
result6_.tm_isdst = 0;
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);
gettimeofday(&tv1, NULL);
for (i=0; i<M; ++i)
{
struct tm result7_;
memcpy(&result7_, &result7, sizeof(result7_));
result7_.tm_isdst = -1;
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)
{
struct tm result8_;
memcpy(&result8_, &result8, sizeof(result8_));
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);
gettimeofday(&tv1, NULL);
for (i=0; i<M; ++i)
{
struct tm result9_;
memcpy(&result9_, &result9, sizeof(result9_));
result9_.tm_isdst = 1;
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);
gettimeofday(&tv1, NULL);
for (i=0; i<M; ++i)
{
struct tm result10_;
memcpy(&result10_, &result10, sizeof(result10_));
result10_.tm_isdst = 0;
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);
gettimeofday(&tv1, NULL);
for (i=0; i<M; ++i)
{
struct tm result11_;
memcpy(&result11_, &result11, sizeof(result11_));
result11_.tm_isdst = -1;
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)
{
struct tm result12_;
memcpy(&result12_, &result12, sizeof(result12_));
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 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义