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

时间:2015-07-16 18:42:57

标签: c++ c performance

我正在练习我的C& 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;
}

2 个答案:

答案 0 :(得分: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个字符,这就是大量的堆栈使用,并且大部分执行时间都花在调用,操作堆栈帧和返回上,而花费很少的时间来交换字符。

这里的递归是“隐藏在无处”。

答案 1 :(得分:0)

C ++递归函数非常糟糕:使用迭代器可以提高速度并且代码更清晰:

g++ test.cpp -lmpfr -lgmp