#include #include #include #include #include char *readFile(size_t *len) { static char buf[1024]; FILE *file = fopen("data.txt", "rb"); *len = fread(buf, 1, 1024, file); fclose(file); return buf; } int setlocale(const char *loc) { char *locnew; locnew = setlocale(LC_ALL, loc); if (locnew == NULL) { printf("setlocale failed.\n"); return 0; } return 1; } char *gbk2utf(iconv_t ct, char *gbk, size_t *len) { static char _buf[1024]; char *inBuf = _buf; size_t bufLen = sizeof(inBuf); iconv(ct, &gbk, len, &inBuf, &bufLen); *len = bufLen; return _buf; } void dumpBytes(const char *bytes, size_t len) { while (len--) { printf("0x%02X,", (unsigned char)*bytes++); } printf("\n"); } void convert(int count) { size_t len; char *gbk = readFile(&len); char *utf; iconv_t ct = iconv_open("UTF-8", "GBK"); size_t gbkLen; clock_t t1 = clock(); for (int idx = 0; idx < count; ++idx) { gbkLen = len; utf = gbk2utf(ct, gbk, &gbkLen); } clock_t t2 = clock(); iconv_close(ct); printf("CLOCKS_PER_SECOND is %d.\n", CLOCKS_PER_SEC); printf("cost is %d, average is %f.\n", t2 - t1, (t2 - t1)/count/CLOCKS_PER_SEC); len = gbkLen; printf("data len is %d.\n", len); printf("data is %s.\n", utf); } int main(int argc, char **argv) { int count = atoi(argv[1]); convert(count); #ifdef _MSC_VER getchar(); #endif return EXIT_SUCCESS; }