使用交换和递归的 C 和C++ 中的字符串反向性能

String reverse performance in C & C++ using swapping and recursion

本文关键字:字符串 性能 交换 递归 C++      更新时间:2023-10-16

我在练习C &然后我决定用在这两种语言中使用的方法来做字符串反转问题。我写了一个递归解和索引方法。这里有4个逆函数;2个使用严格的C方法进行计算,另外2个使用c++ (STL, String, algorithm)调用。

  • 这是一个很好的比较,看看每个方法的速度还是我遗漏了什么东西?
  • 我还想知道每个人有多少内存方法使用,但我还没有想出如何做到这一点。
// C++ reverse string
#include <string> // string
#include <algorithm> // reverse
#include <iostream> // cout
#include <cstring> // std::strcpy
#include <stdio.h> // printf
#include <sys/time.h> // gettimeofday
inline void swap_characters(char* left, char* right) {
    char temp = *left;
    *left = *right;
    *right = temp;
}
void c_index_reverse(char* input, size_t inputSize) {
    const size_t strSize = inputSize - 1;
    char temp;
    for(int i=0 ; i < inputSize / 2 ; i++) {
        swap_characters(&input[i], &input[strSize - i]);
    }
}
void c_recursive_reverse(char str[], int index, int size)
{
    swap_characters(&str[index], &str[size - index]);
    if (index == size / 2)
        return;
    c_recursive_reverse(str, index + 1, size);
}

void c_plusplus_index_reverse(std::string& input) {
    const size_t strSize = input.length();
    for(int i=0 ; i < strSize / 2 ; i++)
        std::swap(input[i], input[strSize - i - 1]);
}

std::string c_plusplus_recursive_reverse(std::string& input) {
    if(input.length() <= 1) {
        return input;
    }
    std::string tmp = std::string(input.begin() + 1, input.end());
    return c_plusplus_recursive_reverse(tmp) + input[0];
}

double timeit(struct timeval &start, struct timeval &end){
    double delta = ((end.tv_sec - start.tv_sec) * 1000000u + end.tv_usec - start.tv_usec) / 1.e6;
    return delta;
}
int main() {
    struct timeval start,end;
    // using C++ STL
    std::string temp = "something very weird is another word that includes a longer text to see the delay" 
    "something very weird is another word that includes a longer text to see the delay" 
    "something very weird is another word that includes a longer text to see the delay" 
    "something very weird is another word that includes a longer text to see the delay"  
    "something very weird is another word that includes a longer text to see the delay" 
    "something very weird is another word that includes a longer text to see the delay" 
    "something very weird is another word that includes a longer text to see the delay";
    std::cout << temp << std::endl;
    // using c++ recursive reverse function - 4
    gettimeofday(&start, NULL);
    std::reverse(temp.begin(), temp.end());
    gettimeofday(&end, NULL);
    std::cout << temp << std::endl;
    printf("%lf n",timeit(start, end));

    // use C++ style functions
    // using recersive - 5
    gettimeofday(&start, NULL);
    temp = c_plusplus_recursive_reverse(temp);
    gettimeofday(&end, NULL);
    std::cout << temp  << std::endl;
    printf("%lf n",timeit(start, end));
    // using index reverse - 3
    gettimeofday(&start, NULL);
    c_plusplus_index_reverse(temp);
    gettimeofday(&end, NULL);
    std::cout << temp << std::endl;
    printf("%lf n",timeit(start, end));

    // Now do C style
    char *cStr = new char[temp.length() + 1];
    std::strcpy(cStr, temp.c_str());

    // using index - 1
    gettimeofday(&start, NULL);
    c_index_reverse(cStr, temp.length());
    gettimeofday(&end, NULL);
    printf("%s n", cStr);
    printf("%lf n",timeit(start, end));

    // using recersive - 2
    gettimeofday(&start, NULL);
    c_recursive_reverse(cStr, 0, temp.length() - 1);
    gettimeofday(&end, NULL);
    printf("%s n", cStr);
    printf("%lf n",timeit(start, end));

    return 0;
}

拥有一个内联函数swap_characters()是一个只有三行代码的大工作,当你已经拥有它们时创建更多的指针(尽管编译器可能会优化)。使用另一个索引变量,比如j,它会递减,直到它满足i,这样会更有效率。

void c_index_reverse(char* input, size_t inputSize) {
    int j = inputSize - 1;
    char temp;
    for(int i=0; i<j; i++) {
        temp = input[i];
        input[i] = input[j];
        input[j--] = temp;
    }
}

"我还想知道每个方法使用多少内存"。非递归方法使用最小的内存,因为它只使用指针和索引器。但是递归方法使用更多的内存,因为每个字符串字符(最多为字符串长度的一半)都会调用递归,因此会使用更多的堆栈。由于您的字符串"something very weird ..."有大约600个字符,这是大量的堆栈使用,大部分执行时间花在调用,操作堆栈帧和返回上,只有很少的时间花在交换字符上。

这里的递归是"无处隐藏"

c++递归函数真的很糟糕:使用迭代器提高了速度,代码更干净:

void c_plusplus_recursive_reverse(std::string::iterator start, 
    std::string::iterator end) 
{
    if(start >= end) {
        return;
    }
    std::iter_swap(start, end);
    c_plusplus_recursive_swap_reverse(++start, --end);
}