[英]String reverse performance in C & C++ using swapping and recursion
我當時正在練習C&C ++技能,然后決定使用兩種語言中使用的方法來解決字符串反向問題。 我寫了一個遞歸解決方案和索引方法。 這里有4個反向功能; 2個使用嚴格的C方法進行計算,其他2個使用C ++(STL,String,算法)調用。
// 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()
僅對三行代碼起作用,在已經擁有它們的情況下創建更多的指針(盡管編譯器可能會優化)。 使用另一個索引變量var,例如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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.