使用交换和递归的 C 和C++ 中的字符串反向性能
String reverse performance in C & C++ using swapping and recursion
我在练习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);
}
相关文章:
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 通过默认复制构造函数比较 C++ 字符串是否会影响性能,原因为何?
- 性能 - 使用字符串构造函数与使用串联
- C++和Java的字符串循环性能比较
- 检查从查询返回的任何行是否包含在字符串中的最高性能方法?
- MongoC ++驱动程序BSON构造:基于流与基于字符串解析.哪一个性能更好?
- STD ::移动性能,同时将大字符串作为rvalue
- C++按字符串调用函数,比较PHP的性能,如何在C++中优化代码
- 性能字符串作为函数参数
- C++字符串构造性能
- 字符串操作性能问题
- 字符串与哈希作为映射键 - 性能
- 通过 iStringStream 标记化的 C++ 字符串的性能开销
- 对于静态错误字符串,哪个的内存/性能效率更高,或者有替代方案
- std::用于处理短字符串的字符串性能
- 在将 char 数组复制到字符串期间节省 CPU 周期(提高性能)
- 性能字符串与字符串
- C++代码性能字符串进行比较
- 性能标准::strstr vs. 标准::字符串::查找
- 字符串到flywweights的字符串转换:更好的性能选项