#include <stdio.h> #include <string.h> #include <time.h>

char* reverse(char* str);

int main() { clock_t start = clock();

char str[] = 'hello, world!';
for (int i = 0; i < 10000000; i++) {
    char* tempStr = strstr(str, 'o');
    while (tempStr != NULL) {
        *tempStr = '0';
        tempStr = strstr(tempStr + 1, 'o');
    }

    tempStr = strstr(str, 'l');
    while (tempStr != NULL) {
        *tempStr = '1';
        tempStr = strstr(tempStr + 1, 'l');
    }

    tempStr = strstr(str, ',');
    while (tempStr != NULL) {
        memmove(tempStr, tempStr + 1, strlen(tempStr));
        tempStr = strstr(tempStr, ',');
    }

    tempStr = strstr(str, '!');
    while (tempStr != NULL) {
        memmove(tempStr, tempStr + 1, strlen(tempStr));
        tempStr = strstr(tempStr, '!');
    }

    reverse(str);
}

clock_t end = clock();
printf("C语言程序运行时间:%ldms\n", (end - start) * 1000 / CLOCKS_PER_SEC);

return 0;

}

char* reverse(char* str) { int length = strlen(str); for (int i = 0, j = length - 1; i < j; i++, j--) { char temp = str[i]; str[i] = str[j]; str[j] = temp; } return str; }

Golang 代码转 C 语言:字符串替换和反转性能比较

原文地址: https://www.cveoy.top/t/topic/ofhF 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录