C++ 中快速将字符串大写字母转为小写的方法
此文章有英文版本/This article is available in English
太长不看版
跳转测试结果:测试结果
结论:
- 如果不开 optimize 使用以下代码进行转换
string strlwr_c_cr(const string& input) {
return string(strlwr((char*)input.c_str()));
}
- 如果开 optimize 使用以下代码进行转换
string strlwr_a_cr(const string& input) {
string result = input;
for (size_t i = 0; i < result.size(); ++i) {
if (result[i] >= 'A' && result[i] <= 'Z') {
result[i] += 32;
}
}
return result;
}
- 形参
const string& str
一般来说比string str
快
测试代码
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#include <algorithm> // transform
#include <chrono> // chrono
#include <cstring> // strcpy
#include <iomanip>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
using ptr = string (*)(string);
using ptr_cr = string (*)(const string&);
// typedef string (*ptr)(string str);
// typedef string (*ptr_cr)(const string& str);
struct func_info {
ptr func;
string name;
};
struct func_info_cr {
ptr_cr func;
string name;
};
vector<pair<string, int64_t>> results;
map<string, int> winner;
string strlwr_a_cr(const string& input) {
string result = input;
for (size_t i = 0; i < result.size(); ++i) {
if (result[i] >= 'A' && result[i] <= 'Z') {
result[i] += 32;
}
}
return result;
}
string strlwr_a(string input) {
for (size_t i = 0; i < input.size(); ++i) {
if (input[i] >= 'A' && input[i] <= 'Z') {
input[i] += 32;
}
}
return input;
}
string strlwr_a_bitwise_operation_cr(const string& input) {
string result = input;
for (size_t i = 0; i < result.size(); ++i) {
if (result[i] >= 'A' && result[i] <= 'Z') {
result[i] |= 32;
}
}
return result;
}
string strlwr_a_bitwise_operation(string input) {
for (size_t i = 0; i < input.size(); ++i) {
if (input[i] >= 'A' && input[i] <= 'Z') {
input[i] |= 32;
}
}
return input;
}
string strlwr_a_tolower_cr(const string& input) {
string result = input;
for (size_t i = 0; i < result.size(); ++i) {
if (result[i] >= 'A' && result[i] <= 'Z') {
result[i] = tolower(result[i]);
}
}
return result;
}
string strlwr_a_tolower(string input) {
for (size_t i = 0; i < input.size(); ++i) {
if (input[i] >= 'A' && input[i] <= 'Z') {
input[i] = tolower(input[i]);
}
}
return input;
}
string strlwr_b_cr(const string& input) {
string result = input;
// +1 是为了存放字符串结束符
// 不加这个测试长字符串和空字符串会 segmentation fault
char cstr[result.size() + 1];
strcpy(cstr, result.c_str());
strlwr(cstr);
return string(cstr);
}
string strlwr_b(string str) {
char cstr[str.size()];
strcpy(cstr, str.c_str());
strlwr(cstr);
return string(cstr);
}
string strlwr_b_plus1(string str) {
// +1 是为了存放字符串结束符
char cstr[str.size() + 1];
strcpy(cstr, str.c_str());
strlwr(cstr);
return string(cstr);
}
char cstr_array[100000001];
string strlwr_b_gobal_array_cr(const string& input) {
string result = input;
strcpy(cstr_array, result.c_str());
strlwr(cstr_array);
return string(cstr_array);
}
string strlwr_b_gobal_array(string str) {
strcpy(cstr_array, str.c_str());
strlwr(cstr_array);
return string(cstr_array);
}
string strlwr_c_cr(const string& input) {
return string(strlwr((char*)input.c_str()));
}
string strlwr_c(string str) { return string(strlwr((char*)str.c_str())); }
string strlwr_d_cr(const string& input) {
string result = input;
std::transform(result.begin(), result.end(), result.begin(), ::tolower);
return result;
}
string strlwr_d(string str) {
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
return str;
}
string strlwr_e_cr(const string& input) {
string str = input;
std::for_each(str.begin(), str.end(), [](char& c) { c = ::tolower(c); });
return str;
}
string strlwr_e(string str) {
std::for_each(str.begin(), str.end(), [](char& c) { c = ::tolower(c); });
return str;
}
string strlwr_f_cr(const string& input) {
stringstream ss;
for (char c : input) {
ss << static_cast<char>(tolower(c));
}
return ss.str();
}
string strlwr_f(string str) {
stringstream ss;
for (char c : str) {
ss << static_cast<char>(tolower(c));
}
return ss.str();
}
string strlwr_g_cr(const string& input) {
string str = input;
for (auto it = str.begin(); it != str.end(); it++) {
*it = tolower(*it);
}
return str;
}
string strlwr_g(string str) {
for (auto it = str.begin(); it != str.end(); it++) {
*it = tolower(*it);
}
return str;
}
void testfunc(ptr_cr func, const string& func_name, const string& str,
int count) {
auto start = chrono::high_resolution_clock::now();
for (int i = 0; i < count; ++i) {
func(str);
}
auto duration = chrono::duration_cast<chrono::microseconds>(
chrono::high_resolution_clock::now() - start);
results.push_back({func_name, duration.count()});
}
void testfunc(ptr func, const string& func_name, const string& str, int count) {
auto start = chrono::high_resolution_clock::now();
for (int i = 0; i < count; ++i) {
func(str);
}
auto duration = chrono::duration_cast<chrono::microseconds>(
chrono::high_resolution_clock::now() - start);
results.push_back({func_name, duration.count()});
}
void print_results(int count) {
cout << "Results:" << endl;
sort(results.begin(), results.end(),
[](const pair<string, int64_t>& a, const pair<string, int64_t>& b) {
return a.second < b.second;
});
for (const auto& result : results) {
cout << setw(7) << result.second << "ms" << " avg:" << fixed
<< setprecision(4) << 1.0 * result.second / count << " "
<< result.first << endl;
}
auto min_time = min_element(
results.begin(), results.end(),
[](const pair<string, int64_t>& a, const pair<string, int64_t>& b) {
return a.second < b.second;
});
winner[min_time->first]++;
cout << "The fastest function is " << min_time->first << " with time "
<< min_time->second << " ms" << endl;
}
vector<func_info> func_list = {
func_info{strlwr_a, "strlwr_a"},
func_info{strlwr_a_tolower, "strlwr_a_tolower"},
func_info{strlwr_a_bitwise_operation, "strlwr_a_bitwise_operation"},
func_info{strlwr_b, "strlwr_b"},
func_info{strlwr_b_plus1, "strlwr_b_plus1"},
func_info{strlwr_b_gobal_array, "strlwr_b_gobal_array"},
func_info{strlwr_c, "strlwr_c"},
func_info{strlwr_d, "strlwr_d"},
func_info{strlwr_e, "strlwr_e"},
func_info{strlwr_f, "strlwr_f"},
func_info{strlwr_g, "strlwr_g"}};
vector<func_info_cr> func_list_cr = {
func_info_cr{strlwr_a_cr, "strlwr_a_cr"},
func_info_cr{strlwr_a_tolower_cr, "strlwr_a_tolower_cr"},
func_info_cr{strlwr_a_bitwise_operation_cr,
"strlwr_a_bitwise_operation_cr"},
func_info_cr{strlwr_b_cr, "strlwr_b_cr"},
func_info_cr{strlwr_b_gobal_array_cr, "strlwr_b_gobal_array_cr"},
func_info_cr{strlwr_c_cr, "strlwr_c_cr"},
func_info_cr{strlwr_d_cr, "strlwr_d_cr"},
func_info_cr{strlwr_e_cr, "strlwr_e_cr"},
func_info_cr{strlwr_f_cr, "strlwr_f_cr"},
func_info_cr{strlwr_g_cr, "strlwr_g_cr"}};
void test_func_list(const string& str, int count) {
for (const auto& func : func_list) {
testfunc(func.func, func.name, str, count);
}
for (const auto& func : func_list_cr) {
testfunc(func.func, func.name, str, count);
}
print_results(count);
results.clear();
cout << endl;
}
int main() {
cout << "Test with empty string" << endl;
test_func_list("", 1000001);
cout << "Test with single character: H" << endl;
test_func_list("H", 1000001);
cout << "Test with two characters: AH" << endl;
test_func_list("AH", 1000001);
cout << "Test with three characters: AHA" << endl;
test_func_list("AHA", 1000001);
cout << "Test with four characters: HELL" << endl;
test_func_list("HELL", 1000001);
cout << "Test with five characters: Hello" << endl;
test_func_list("Hello", 1000001);
cout << "Test with five characters: $%^&*" << endl;
test_func_list("$%^&*", 1000001);
string long_lower_str(100000, 'a');
string long_dash_str(100000, '-');
cout << "Test with string: string(100000, 'a')" << endl;
test_func_list(long_lower_str, 101);
cout << "Test with string: string(100000, '-')" << endl;
test_func_list(long_dash_str, 101);
cout << "Test with five characters: string(100000, 'a')+'A'+string(100000, "
"'a')"
<< endl;
test_func_list(long_lower_str + "A" + long_lower_str, 101);
cout << "Test with five characters: string(100000, '-')+'A'+string(100000, "
"'-')"
<< endl;
test_func_list(long_dash_str + "A" + long_dash_str, 101);
// 测试普通字符串下的表现
cout << "Test with normal string: Hello, World!" << endl;
test_func_list("Hello, World!", 1000001);
// 测试长字符下的表现
cout << "Test with long string: string(100000, 'A')" << endl;
string long_str(100000, 'A');
test_func_list(long_str, 101);
// 测试超长字符下的表现
cout << "Test with very long string: string(1000000, 'A')" << endl;
string very_long_str(1000000, 'A');
test_func_list(very_long_str, 11);
// 只处理一次比超长还长的字符串
cout << "Test with super long string: string(10000000, 'A')" << endl;
cout << "Not all functions will be tested, because array is too long"
<< endl;
string super_long_str(100000000, 'A');
// func_list 中移除 strlwr_b 和 strlwr_b_plus1, func_list_cr 中移除
// strlwr_b_cr
func_list.erase(remove_if(func_list.begin(), func_list.end(),
[](const func_info& f) {
return f.name == "strlwr_b" ||
f.name == "strlwr_b_plus1";
}),
func_list.end());
func_list_cr.erase(remove_if(func_list_cr.begin(), func_list_cr.end(),
[](const func_info_cr& f) {
return f.name == "strlwr_b_cr";
}),
func_list_cr.end());
test_func_list(super_long_str, 1);
// 输出函数赢得最快的次数
cout << endl << "Winner:" << endl;
for (const auto& w : winner) {
cout << w.first << ": " << w.second << endl;
}
return 0;
}
测试结果
Test with empty string
Results:
2178ms avg:0.0022 strlwr_a_cr
2365ms avg:0.0024 strlwr_e_cr
2382ms avg:0.0024 strlwr_a_tolower_cr
2390ms avg:0.0024 strlwr_a_bitwise_operation_cr
2501ms avg:0.0025 strlwr_g_cr
2571ms avg:0.0026 strlwr_d_cr
3575ms avg:0.0036 strlwr_a
3642ms avg:0.0036 strlwr_d
3692ms avg:0.0037 strlwr_e
3755ms avg:0.0038 strlwr_a_bitwise_operation
3766ms avg:0.0038 strlwr_g
4184ms avg:0.0042 strlwr_a_tolower
4810ms avg:0.0048 strlwr_c_cr
5994ms avg:0.0060 strlwr_c
8125ms avg:0.0081 strlwr_b_gobal_array
8144ms avg:0.0081 strlwr_b_plus1
8326ms avg:0.0083 strlwr_b
9109ms avg:0.0091 strlwr_b_cr
9113ms avg:0.0091 strlwr_b_gobal_array_cr
35442ms avg:0.0354 strlwr_f_cr
37052ms avg:0.0371 strlwr_f
The fastest function is strlwr_a_cr with time 2178 ms
Test with single character: H
Results:
2020ms avg:0.0020 strlwr_a_cr
2624ms avg:0.0026 strlwr_a_bitwise_operation_cr
2983ms avg:0.0030 strlwr_g_cr
3369ms avg:0.0034 strlwr_e_cr
3373ms avg:0.0034 strlwr_d_cr
3436ms avg:0.0034 strlwr_a_tolower_cr
5238ms avg:0.0052 strlwr_c_cr
5743ms avg:0.0057 strlwr_a
5754ms avg:0.0058 strlwr_a_bitwise_operation
5934ms avg:0.0059 strlwr_a_tolower
6040ms avg:0.0060 strlwr_d
6044ms avg:0.0060 strlwr_e
6079ms avg:0.0061 strlwr_c
6099ms avg:0.0061 strlwr_g
8871ms avg:0.0089 strlwr_b_gobal_array
9246ms avg:0.0092 strlwr_b_plus1
9539ms avg:0.0095 strlwr_b
9897ms avg:0.0099 strlwr_b_gobal_array_cr
9973ms avg:0.0100 strlwr_b_cr
39362ms avg:0.0394 strlwr_f_cr
41351ms avg:0.0414 strlwr_f
The fastest function is strlwr_a_cr with time 2020 ms
Test with two characters: AH
Results:
4078ms avg:0.0041 strlwr_a_cr
4414ms avg:0.0044 strlwr_a_bitwise_operation_cr
5837ms avg:0.0058 strlwr_e_cr
5848ms avg:0.0058 strlwr_d_cr
5961ms avg:0.0060 strlwr_g_cr
6434ms avg:0.0064 strlwr_a_tolower_cr
6531ms avg:0.0065 strlwr_a_bitwise_operation
6886ms avg:0.0069 strlwr_e
6892ms avg:0.0069 strlwr_c_cr
7289ms avg:0.0073 strlwr_a
7546ms avg:0.0075 strlwr_d
7567ms avg:0.0076 strlwr_g
7607ms avg:0.0076 strlwr_a_tolower
10611ms avg:0.0106 strlwr_c
10743ms avg:0.0107 strlwr_b_gobal_array
11628ms avg:0.0116 strlwr_b
11651ms avg:0.0117 strlwr_b_gobal_array_cr
11874ms avg:0.0119 strlwr_b_cr
12096ms avg:0.0121 strlwr_b_plus1
43736ms avg:0.0437 strlwr_f_cr
46085ms avg:0.0461 strlwr_f
The fastest function is strlwr_a_cr with time 4078 ms
Test with three characters: AHA
Results:
4435ms avg:0.0044 strlwr_a_cr
5064ms avg:0.0051 strlwr_a_bitwise_operation_cr
6667ms avg:0.0067 strlwr_e_cr
6781ms avg:0.0068 strlwr_a_bitwise_operation
6838ms avg:0.0068 strlwr_d_cr
7064ms avg:0.0071 strlwr_g_cr
7205ms avg:0.0072 strlwr_c_cr
7559ms avg:0.0076 strlwr_a
7871ms avg:0.0079 strlwr_a_tolower_cr
8169ms avg:0.0082 strlwr_d
8637ms avg:0.0086 strlwr_a_tolower
8768ms avg:0.0088 strlwr_e
8955ms avg:0.0090 strlwr_g
10290ms avg:0.0103 strlwr_c
12103ms avg:0.0121 strlwr_b
12145ms avg:0.0121 strlwr_b_plus1
12159ms avg:0.0122 strlwr_b_gobal_array
12658ms avg:0.0127 strlwr_b_gobal_array_cr
13500ms avg:0.0135 strlwr_b_cr
45392ms avg:0.0454 strlwr_f_cr
48920ms avg:0.0489 strlwr_f
The fastest function is strlwr_a_cr with time 4435 ms
Test with four characters: HELL
Results:
4797ms avg:0.0048 strlwr_a_cr
5733ms avg:0.0057 strlwr_a_bitwise_operation_cr
7325ms avg:0.0073 strlwr_a_bitwise_operation
7509ms avg:0.0075 strlwr_e_cr
7532ms avg:0.0075 strlwr_d_cr
7896ms avg:0.0079 strlwr_c_cr
8058ms avg:0.0081 strlwr_a
8224ms avg:0.0082 strlwr_g_cr
8812ms avg:0.0088 strlwr_a_tolower_cr
9490ms avg:0.0095 strlwr_e
9815ms avg:0.0098 strlwr_d
10267ms avg:0.0103 strlwr_g
10596ms avg:0.0106 strlwr_a_tolower
11059ms avg:0.0111 strlwr_c
13147ms avg:0.0131 strlwr_b_gobal_array
13301ms avg:0.0133 strlwr_b_plus1
13447ms avg:0.0134 strlwr_b_cr
13702ms avg:0.0137 strlwr_b_gobal_array_cr
13866ms avg:0.0139 strlwr_b
47457ms avg:0.0475 strlwr_f_cr
51810ms avg:0.0518 strlwr_f
The fastest function is strlwr_a_cr with time 4797 ms
Test with five characters: Hello
Results:
5167ms avg:0.0052 strlwr_a_cr
5421ms avg:0.0054 strlwr_a_bitwise_operation_cr
5862ms avg:0.0059 strlwr_a_tolower_cr
6957ms avg:0.0070 strlwr_a
7249ms avg:0.0072 strlwr_a_bitwise_operation
7880ms avg:0.0079 strlwr_a_tolower
8073ms avg:0.0081 strlwr_c_cr
8665ms avg:0.0087 strlwr_d_cr
8854ms avg:0.0089 strlwr_e_cr
10173ms avg:0.0102 strlwr_d
10574ms avg:0.0106 strlwr_e
11124ms avg:0.0111 strlwr_g_cr
11872ms avg:0.0119 strlwr_c
12533ms avg:0.0125 strlwr_g
13066ms avg:0.0131 strlwr_b_gobal_array_cr
13387ms avg:0.0134 strlwr_b_gobal_array
13761ms avg:0.0138 strlwr_b_cr
13935ms avg:0.0139 strlwr_b
14094ms avg:0.0141 strlwr_b_plus1
51904ms avg:0.0519 strlwr_f_cr
53267ms avg:0.0533 strlwr_f
The fastest function is strlwr_a_cr with time 5167 ms
Test with five characters: $%^&*
Results:
5141ms avg:0.0051 strlwr_a_cr
5315ms avg:0.0053 strlwr_a_tolower_cr
5338ms avg:0.0053 strlwr_a_bitwise_operation_cr
7007ms avg:0.0070 strlwr_a_tolower
7120ms avg:0.0071 strlwr_a_bitwise_operation
7328ms avg:0.0073 strlwr_a
8235ms avg:0.0082 strlwr_c_cr
8709ms avg:0.0087 strlwr_d_cr
8849ms avg:0.0088 strlwr_e_cr
10637ms avg:0.0106 strlwr_e
10723ms avg:0.0107 strlwr_d
11173ms avg:0.0112 strlwr_c
11375ms avg:0.0114 strlwr_g_cr
12762ms avg:0.0128 strlwr_g
13200ms avg:0.0132 strlwr_b_gobal_array_cr
13285ms avg:0.0133 strlwr_b_gobal_array
13724ms avg:0.0137 strlwr_b_cr
14110ms avg:0.0141 strlwr_b_plus1
14218ms avg:0.0142 strlwr_b
50909ms avg:0.0509 strlwr_f_cr
53042ms avg:0.0530 strlwr_f
The fastest function is strlwr_a_cr with time 5141 ms
Test with string: string(100000, 'a')
Results:
2789ms avg:27.6139 strlwr_a_tolower
3246ms avg:32.1386 strlwr_a
3339ms avg:33.0594 strlwr_a_bitwise_operation_cr
3594ms avg:35.5842 strlwr_a_cr
3938ms avg:38.9901 strlwr_c_cr
4092ms avg:40.5149 strlwr_a_tolower_cr
5577ms avg:55.2178 strlwr_a_bitwise_operation
8447ms avg:83.6337 strlwr_b_gobal_array_cr
9119ms avg:90.2871 strlwr_b_cr
9485ms avg:93.9109 strlwr_d_cr
9487ms avg:93.9307 strlwr_e_cr
9587ms avg:94.9208 strlwr_c
9812ms avg:97.1485 strlwr_b
9856ms avg:97.5842 strlwr_b_gobal_array
10366ms avg:102.6337 strlwr_b_plus1
11705ms avg:115.8911 strlwr_d
11967ms avg:118.4851 strlwr_e
15203ms avg:150.5248 strlwr_g_cr
15375ms avg:152.2277 strlwr_g
31721ms avg:314.0693 strlwr_f_cr
34112ms avg:337.7426 strlwr_f
The fastest function is strlwr_a_tolower with time 2789 ms
Test with string: string(100000, '-')
Results:
2662ms avg:26.3564 strlwr_a_cr
2699ms avg:26.7228 strlwr_a_bitwise_operation_cr
3001ms avg:29.7129 strlwr_a_bitwise_operation
3023ms avg:29.9307 strlwr_c_cr
3135ms avg:31.0396 strlwr_a_tolower_cr
3395ms avg:33.6139 strlwr_a_tolower
4217ms avg:41.7525 strlwr_a
5208ms avg:51.5644 strlwr_b_cr
5306ms avg:52.5347 strlwr_b_gobal_array_cr
6695ms avg:66.2871 strlwr_c
6919ms avg:68.5050 strlwr_b_plus1
7002ms avg:69.3267 strlwr_b_gobal_array
7757ms avg:76.8020 strlwr_b
8573ms avg:84.8812 strlwr_d_cr
8707ms avg:86.2079 strlwr_e_cr
8908ms avg:88.1980 strlwr_e
9060ms avg:89.7030 strlwr_d
14566ms avg:144.2178 strlwr_g_cr
14701ms avg:145.5545 strlwr_g
27187ms avg:269.1782 strlwr_f_cr
31603ms avg:312.9010 strlwr_f
The fastest function is strlwr_a_cr with time 2662 ms
Test with five characters: string(100000, 'a')+'A'+string(100000, 'a')
Results:
4513ms avg:44.6832 strlwr_a_bitwise_operation_cr
4611ms avg:45.6535 strlwr_a_cr
4930ms avg:48.8119 strlwr_a_bitwise_operation
5399ms avg:53.4554 strlwr_c_cr
5454ms avg:54.0000 strlwr_a_tolower_cr
5471ms avg:54.1683 strlwr_c
5635ms avg:55.7921 strlwr_a_tolower
5678ms avg:56.2178 strlwr_a
6460ms avg:63.9604 strlwr_b_gobal_array
6560ms avg:64.9505 strlwr_b_gobal_array_cr
6684ms avg:66.1782 strlwr_b_cr
6760ms avg:66.9307 strlwr_b_plus1
7922ms avg:78.4356 strlwr_b
16758ms avg:165.9208 strlwr_e
16847ms avg:166.8020 strlwr_d
17380ms avg:172.0792 strlwr_d_cr
17451ms avg:172.7822 strlwr_e_cr
28122ms avg:278.4356 strlwr_g_cr
29125ms avg:288.3663 strlwr_g
44937ms avg:444.9208 strlwr_f_cr
51669ms avg:511.5743 strlwr_f
The fastest function is strlwr_a_bitwise_operation_cr with time 4513 ms
Test with five characters: string(100000, '-')+'A'+string(100000, '-')
Results:
4563ms avg:45.1782 strlwr_a
4589ms avg:45.4356 strlwr_a_bitwise_operation_cr
4660ms avg:46.1386 strlwr_a_bitwise_operation
4670ms avg:46.2376 strlwr_a_cr
5081ms avg:50.3069 strlwr_c_cr
5483ms avg:54.2871 strlwr_a_tolower_cr
5524ms avg:54.6931 strlwr_a_tolower
5571ms avg:55.1584 strlwr_c
6308ms avg:62.4554 strlwr_b
6349ms avg:62.8614 strlwr_b_gobal_array
6379ms avg:63.1584 strlwr_b_gobal_array_cr
6871ms avg:68.0297 strlwr_b_plus1
6884ms avg:68.1584 strlwr_b_cr
16997ms avg:168.2871 strlwr_d_cr
17099ms avg:169.2970 strlwr_d
17176ms avg:170.0594 strlwr_e_cr
17382ms avg:172.0990 strlwr_e
23653ms avg:234.1881 strlwr_g
28559ms avg:282.7624 strlwr_g_cr
44610ms avg:441.6832 strlwr_f
46119ms avg:456.6238 strlwr_f_cr
The fastest function is strlwr_a with time 4563 ms
Test with normal string: Hello, World!
Results:
5914ms avg:0.0059 strlwr_a_cr
7619ms avg:0.0076 strlwr_a_bitwise_operation_cr
7974ms avg:0.0080 strlwr_a_tolower_cr
8244ms avg:0.0082 strlwr_a
8715ms avg:0.0087 strlwr_c_cr
9164ms avg:0.0092 strlwr_a_bitwise_operation
10048ms avg:0.0100 strlwr_a_tolower
11419ms avg:0.0114 strlwr_c
13792ms avg:0.0138 strlwr_b_gobal_array
13879ms avg:0.0139 strlwr_b_gobal_array_cr
14093ms avg:0.0141 strlwr_d_cr
14145ms avg:0.0141 strlwr_b_plus1
14202ms avg:0.0142 strlwr_e_cr
14854ms avg:0.0149 strlwr_b
14883ms avg:0.0149 strlwr_b_cr
15807ms avg:0.0158 strlwr_d
15816ms avg:0.0158 strlwr_e
16930ms avg:0.0169 strlwr_g_cr
23235ms avg:0.0232 strlwr_g
68491ms avg:0.0685 strlwr_f_cr
70159ms avg:0.0702 strlwr_f
The fastest function is strlwr_a_cr with time 5914 ms
Test with long string: string(100000, 'A')
Results:
2850ms avg:28.2178 strlwr_c_cr
4090ms avg:40.4950 strlwr_a_bitwise_operation
4148ms avg:41.0693 strlwr_a_bitwise_operation_cr
4247ms avg:42.0495 strlwr_a_cr
4617ms avg:45.7129 strlwr_c
5144ms avg:50.9307 strlwr_b_gobal_array_cr
5148ms avg:50.9703 strlwr_b_gobal_array
5162ms avg:51.1089 strlwr_b_plus1
5244ms avg:51.9208 strlwr_b_cr
5251ms avg:51.9901 strlwr_b
5647ms avg:55.9109 strlwr_a
8358ms avg:82.7525 strlwr_e_cr
8392ms avg:83.0891 strlwr_d
8481ms avg:83.9703 strlwr_e
8549ms avg:84.6436 strlwr_d_cr
12205ms avg:120.8416 strlwr_a_tolower_cr
12422ms avg:122.9901 strlwr_a_tolower
14142ms avg:140.0198 strlwr_g_cr
14491ms avg:143.4752 strlwr_g
20935ms avg:207.2772 strlwr_f_cr
22663ms avg:224.3861 strlwr_f
The fastest function is strlwr_c_cr with time 2850 ms
Test with very long string: string(1000000, 'A')
Results:
3327ms avg:302.4545 strlwr_c_cr
3703ms avg:336.6364 strlwr_a_bitwise_operation
4528ms avg:411.6364 strlwr_a_cr
4606ms avg:418.7273 strlwr_a_bitwise_operation_cr
6157ms avg:559.7273 strlwr_a
7766ms avg:706.0000 strlwr_c
8222ms avg:747.4545 strlwr_b_plus1
8304ms avg:754.9091 strlwr_b_gobal_array_cr
8755ms avg:795.9091 strlwr_b_cr
9240ms avg:840.0000 strlwr_e_cr
9245ms avg:840.4545 strlwr_d_cr
9280ms avg:843.6364 strlwr_d
9355ms avg:850.4545 strlwr_e
9403ms avg:854.8182 strlwr_b_gobal_array
9592ms avg:872.0000 strlwr_b
13051ms avg:1186.4545 strlwr_a_tolower
13186ms avg:1198.7273 strlwr_a_tolower_cr
15767ms avg:1433.3636 strlwr_g
15837ms avg:1439.7273 strlwr_g_cr
28014ms avg:2546.7273 strlwr_f_cr
29226ms avg:2656.9091 strlwr_f
The fastest function is strlwr_c_cr with time 3327 ms
Test with super long string: string(10000000, 'A')
Not all functions will be tested, because array is too long
Results:
51371ms avg:51371.0000 strlwr_a_bitwise_operation
52663ms avg:52663.0000 strlwr_a
53637ms avg:53637.0000 strlwr_a_cr
57451ms avg:57451.0000 strlwr_c_cr
57926ms avg:57926.0000 strlwr_a_bitwise_operation_cr
71125ms avg:71125.0000 strlwr_c
78141ms avg:78141.0000 strlwr_b_gobal_array_cr
98493ms avg:98493.0000 strlwr_b_gobal_array
99366ms avg:99366.0000 strlwr_e
99485ms avg:99485.0000 strlwr_d
99658ms avg:99658.0000 strlwr_e_cr
101898ms avg:101898.0000 strlwr_d_cr
113328ms avg:113328.0000 strlwr_a_tolower
117965ms avg:117965.0000 strlwr_a_tolower_cr
132375ms avg:132375.0000 strlwr_g_cr
151730ms avg:151730.0000 strlwr_g
262515ms avg:262515.0000 strlwr_f_cr
282339ms avg:282339.0000 strlwr_f
The fastest function is strlwr_a_bitwise_operation with time 51371 ms
Winner:
strlwr_a: 1
strlwr_a_bitwise_operation: 1
strlwr_a_bitwise_operation_cr: 1
strlwr_a_cr: 9
strlwr_a_tolower: 1
strlwr_c_cr: 2
不加开头 `#pragma GCC optimize` 的运行结果
Test with empty string
Results:
3378ms avg:0.0034 strlwr_a_tolower_cr
3618ms avg:0.0036 strlwr_a_cr
3979ms avg:0.0040 strlwr_a_bitwise_operation_cr
5598ms avg:0.0056 strlwr_a_tolower
5930ms avg:0.0059 strlwr_a_bitwise_operation
6937ms avg:0.0069 strlwr_g_cr
7202ms avg:0.0072 strlwr_a
7728ms avg:0.0077 strlwr_e_cr
8098ms avg:0.0081 strlwr_d_cr
9078ms avg:0.0091 strlwr_g
9863ms avg:0.0099 strlwr_e
10576ms avg:0.0106 strlwr_d
15573ms avg:0.0156 strlwr_c_cr
17512ms avg:0.0175 strlwr_c
18659ms avg:0.0187 strlwr_b_gobal_array_cr
18686ms avg:0.0187 strlwr_b_gobal_array
19948ms avg:0.0199 strlwr_b
20400ms avg:0.0204 strlwr_b_cr
20450ms avg:0.0204 strlwr_b_plus1
37561ms avg:0.0376 strlwr_f_cr
40086ms avg:0.0401 strlwr_f
The fastest function is strlwr_a_tolower_cr with time 3378 ms
Test with single character: H
Results:
7521ms avg:0.0075 strlwr_a_cr
8254ms avg:0.0083 strlwr_a_bitwise_operation_cr
8490ms avg:0.0085 strlwr_a_tolower_cr
10662ms avg:0.0107 strlwr_a
11120ms avg:0.0111 strlwr_a_tolower
11400ms avg:0.0114 strlwr_a_bitwise_operation
13333ms avg:0.0133 strlwr_e_cr
14615ms avg:0.0146 strlwr_g_cr
14935ms avg:0.0149 strlwr_d_cr
15505ms avg:0.0155 strlwr_e
15751ms avg:0.0158 strlwr_c_cr
16225ms avg:0.0162 strlwr_g
17185ms avg:0.0172 strlwr_d
17707ms avg:0.0177 strlwr_c
18882ms avg:0.0189 strlwr_b_gobal_array_cr
19347ms avg:0.0193 strlwr_b_gobal_array
20331ms avg:0.0203 strlwr_b_cr
20346ms avg:0.0203 strlwr_b
20521ms avg:0.0205 strlwr_b_plus1
48094ms avg:0.0481 strlwr_f_cr
49518ms avg:0.0495 strlwr_f
The fastest function is strlwr_a_cr with time 7521 ms
Test with two characters: AH
Results:
11510ms avg:0.0115 strlwr_a_bitwise_operation_cr
11860ms avg:0.0119 strlwr_a_cr
13857ms avg:0.0139 strlwr_a_bitwise_operation
14099ms avg:0.0141 strlwr_a
14907ms avg:0.0149 strlwr_a_tolower_cr
16400ms avg:0.0164 strlwr_a_tolower
16698ms avg:0.0167 strlwr_c_cr
19599ms avg:0.0196 strlwr_c
19856ms avg:0.0199 strlwr_e_cr
20785ms avg:0.0208 strlwr_b_gobal_array_cr
20962ms avg:0.0210 strlwr_b_gobal_array
22170ms avg:0.0222 strlwr_b_cr
22627ms avg:0.0226 strlwr_b
22628ms avg:0.0226 strlwr_d_cr
22806ms avg:0.0228 strlwr_g_cr
23036ms avg:0.0230 strlwr_b_plus1
23055ms avg:0.0231 strlwr_e
24483ms avg:0.0245 strlwr_g
25461ms avg:0.0255 strlwr_d
55398ms avg:0.0554 strlwr_f_cr
63354ms avg:0.0634 strlwr_f
The fastest function is strlwr_a_bitwise_operation_cr with time 11510 ms
Test with three characters: AHA
Results:
14940ms avg:0.0149 strlwr_a_bitwise_operation_cr
17139ms avg:0.0171 strlwr_a_cr
17281ms avg:0.0173 strlwr_c_cr
17510ms avg:0.0175 strlwr_a
17700ms avg:0.0177 strlwr_a_bitwise_operation
19438ms avg:0.0194 strlwr_a_tolower_cr
20601ms avg:0.0206 strlwr_c
21439ms avg:0.0214 strlwr_b_gobal_array
21662ms avg:0.0217 strlwr_a_tolower
21761ms avg:0.0218 strlwr_b_gobal_array_cr
23300ms avg:0.0233 strlwr_b
23310ms avg:0.0233 strlwr_b_cr
23831ms avg:0.0238 strlwr_b_plus1
25842ms avg:0.0258 strlwr_e_cr
28901ms avg:0.0289 strlwr_e
29679ms avg:0.0297 strlwr_d_cr
30729ms avg:0.0307 strlwr_g_cr
31678ms avg:0.0317 strlwr_d
31909ms avg:0.0319 strlwr_g
62600ms avg:0.0626 strlwr_f_cr
65815ms avg:0.0658 strlwr_f
The fastest function is strlwr_a_bitwise_operation_cr with time 14940 ms
Test with four characters: HELL
Results:
17787ms avg:0.0178 strlwr_c_cr
18010ms avg:0.0180 strlwr_a_bitwise_operation_cr
19360ms avg:0.0194 strlwr_a_cr
20173ms avg:0.0202 strlwr_a
20609ms avg:0.0206 strlwr_a_bitwise_operation
21292ms avg:0.0213 strlwr_c
21884ms avg:0.0219 strlwr_b_gobal_array
22129ms avg:0.0221 strlwr_b_gobal_array_cr
23611ms avg:0.0236 strlwr_b_cr
23953ms avg:0.0240 strlwr_b_plus1
23990ms avg:0.0240 strlwr_b
24232ms avg:0.0242 strlwr_a_tolower_cr
26410ms avg:0.0264 strlwr_a_tolower
31530ms avg:0.0315 strlwr_e_cr
34225ms avg:0.0342 strlwr_e
35559ms avg:0.0356 strlwr_d_cr
37740ms avg:0.0377 strlwr_g_cr
38481ms avg:0.0385 strlwr_d
39593ms avg:0.0396 strlwr_g
68569ms avg:0.0686 strlwr_f_cr
72244ms avg:0.0722 strlwr_f
The fastest function is strlwr_c_cr with time 17787 ms
Test with five characters: Hello
Results:
18207ms avg:0.0182 strlwr_c_cr
20025ms avg:0.0200 strlwr_a_cr
20525ms avg:0.0205 strlwr_a_bitwise_operation_cr
20683ms avg:0.0207 strlwr_a_tolower_cr
21497ms avg:0.0215 strlwr_c
22813ms avg:0.0228 strlwr_a_bitwise_operation
22877ms avg:0.0229 strlwr_a
22930ms avg:0.0229 strlwr_a_tolower
22975ms avg:0.0230 strlwr_b_gobal_array
23356ms avg:0.0234 strlwr_b_gobal_array_cr
24625ms avg:0.0246 strlwr_b_cr
25111ms avg:0.0251 strlwr_b_plus1
26072ms avg:0.0261 strlwr_b
37079ms avg:0.0371 strlwr_e_cr
40326ms avg:0.0403 strlwr_e
42330ms avg:0.0423 strlwr_d_cr
44254ms avg:0.0443 strlwr_d
45580ms avg:0.0456 strlwr_g_cr
46785ms avg:0.0468 strlwr_g
76473ms avg:0.0765 strlwr_f_cr
79506ms avg:0.0795 strlwr_f
The fastest function is strlwr_c_cr with time 18207 ms
Test with five characters: $%^&*
Results:
15238ms avg:0.0152 strlwr_a_tolower_cr
15438ms avg:0.0154 strlwr_a_bitwise_operation_cr
16300ms avg:0.0163 strlwr_a_cr
17116ms avg:0.0171 strlwr_a_tolower
18042ms avg:0.0180 strlwr_a
18043ms avg:0.0180 strlwr_c_cr
18046ms avg:0.0180 strlwr_a_bitwise_operation
21709ms avg:0.0217 strlwr_c
23199ms avg:0.0232 strlwr_b_gobal_array_cr
23379ms avg:0.0234 strlwr_b_gobal_array
24517ms avg:0.0245 strlwr_b_cr
24884ms avg:0.0249 strlwr_b
25203ms avg:0.0252 strlwr_b_plus1
37744ms avg:0.0377 strlwr_e_cr
40310ms avg:0.0403 strlwr_e
42563ms avg:0.0426 strlwr_d_cr
44388ms avg:0.0444 strlwr_d
45475ms avg:0.0455 strlwr_g_cr
46410ms avg:0.0464 strlwr_g
76417ms avg:0.0764 strlwr_f_cr
80162ms avg:0.0802 strlwr_f
The fastest function is strlwr_a_tolower_cr with time 15238 ms
Test with string: string(100000, 'a')
Results:
3922ms avg:38.8317 strlwr_c_cr
8521ms avg:84.3663 strlwr_b
9219ms avg:91.2772 strlwr_c
9417ms avg:93.2376 strlwr_b_gobal_array_cr
9548ms avg:94.5347 strlwr_b_plus1
9734ms avg:96.3762 strlwr_b_cr
9914ms avg:98.1584 strlwr_b_gobal_array
26039ms avg:257.8119 strlwr_a_bitwise_operation
27266ms avg:269.9604 strlwr_a_tolower_cr
29237ms avg:289.4752 strlwr_a_cr
29496ms avg:292.0396 strlwr_a_tolower
32707ms avg:323.8317 strlwr_a
35212ms avg:348.6337 strlwr_a_bitwise_operation_cr
57861ms avg:572.8812 strlwr_e_cr
58939ms avg:583.5545 strlwr_e
66071ms avg:654.1683 strlwr_d
66352ms avg:656.9505 strlwr_d_cr
75456ms avg:747.0891 strlwr_g
76676ms avg:759.1683 strlwr_g_cr
79081ms avg:782.9802 strlwr_f_cr
79097ms avg:783.1386 strlwr_f
The fastest function is strlwr_c_cr with time 3922 ms
Test with string: string(100000, '-')
Results:
2949ms avg:29.1980 strlwr_c_cr
4983ms avg:49.3366 strlwr_b_gobal_array_cr
5938ms avg:58.7921 strlwr_b_cr
6530ms avg:64.6535 strlwr_b
6683ms avg:66.1683 strlwr_b_plus1
7101ms avg:70.3069 strlwr_c
7597ms avg:75.2178 strlwr_b_gobal_array
18377ms avg:181.9505 strlwr_a_tolower_cr
18599ms avg:184.1485 strlwr_a_bitwise_operation_cr
18695ms avg:185.0990 strlwr_a_tolower
18783ms avg:185.9703 strlwr_a_bitwise_operation
18967ms avg:187.7921 strlwr_a
20637ms avg:204.3267 strlwr_a_cr
56489ms avg:559.2970 strlwr_e_cr
59872ms avg:592.7921 strlwr_e
65280ms avg:646.3366 strlwr_d_cr
65654ms avg:650.0396 strlwr_d
72037ms avg:713.2376 strlwr_f_cr
73377ms avg:726.5050 strlwr_g
76481ms avg:757.2376 strlwr_g_cr
77591ms avg:768.2277 strlwr_f
The fastest function is strlwr_c_cr with time 2949 ms
Test with five characters: string(100000, 'a')+'A'+string(100000, 'a')
Results:
4987ms avg:49.3762 strlwr_c_cr
5412ms avg:53.5842 strlwr_c
6492ms avg:64.2772 strlwr_b_gobal_array_cr
6597ms avg:65.3168 strlwr_b_cr
6633ms avg:65.6733 strlwr_b_gobal_array
7013ms avg:69.4356 strlwr_b_plus1
7057ms avg:69.8713 strlwr_b
52209ms avg:516.9208 strlwr_a_tolower_cr
52833ms avg:523.0990 strlwr_a_bitwise_operation_cr
52858ms avg:523.3465 strlwr_a_tolower
53111ms avg:525.8515 strlwr_a_bitwise_operation
53640ms avg:531.0891 strlwr_a
57055ms avg:564.9010 strlwr_a_cr
114560ms avg:1134.2574 strlwr_e_cr
118714ms avg:1175.3861 strlwr_e
129216ms avg:1279.3663 strlwr_d_cr
129845ms avg:1285.5941 strlwr_d
136271ms avg:1349.2178 strlwr_f_cr
141211ms avg:1398.1287 strlwr_f
145990ms avg:1445.4455 strlwr_g
149732ms avg:1482.4950 strlwr_g_cr
The fastest function is strlwr_c_cr with time 4987 ms
Test with five characters: string(100000, '-')+'A'+string(100000, '-')
Results:
4941ms avg:48.9208 strlwr_c_cr
5110ms avg:50.5941 strlwr_c
6112ms avg:60.5149 strlwr_b_gobal_array
6158ms avg:60.9703 strlwr_b_gobal_array_cr
6223ms avg:61.6139 strlwr_b_plus1
6261ms avg:61.9901 strlwr_b_cr
6338ms avg:62.7525 strlwr_b
36148ms avg:357.9010 strlwr_a_bitwise_operation
36194ms avg:358.3564 strlwr_a_tolower
36202ms avg:358.4356 strlwr_a_tolower_cr
36265ms avg:359.0594 strlwr_a_bitwise_operation_cr
37524ms avg:371.5248 strlwr_a
40249ms avg:398.5050 strlwr_a_cr
112907ms avg:1117.8911 strlwr_e_cr
116405ms avg:1152.5248 strlwr_e
129275ms avg:1279.9505 strlwr_d
131345ms avg:1300.4455 strlwr_d_cr
136273ms avg:1349.2376 strlwr_f_cr
138006ms avg:1366.3960 strlwr_f
150670ms avg:1491.7822 strlwr_g
152053ms avg:1505.4752 strlwr_g_cr
The fastest function is strlwr_c_cr with time 4941 ms
Test with normal string: Hello, World!
Results:
22115ms avg:0.0221 strlwr_c
24015ms avg:0.0240 strlwr_b_gobal_array
25641ms avg:0.0256 strlwr_b_plus1
25746ms avg:0.0257 strlwr_b
36055ms avg:0.0361 strlwr_c_cr
41672ms avg:0.0417 strlwr_a
43151ms avg:0.0432 strlwr_a_bitwise_operation
43893ms avg:0.0439 strlwr_a_tolower
48233ms avg:0.0482 strlwr_b_gobal_array_cr
50032ms avg:0.0500 strlwr_b_cr
53366ms avg:0.0534 strlwr_a_cr
76708ms avg:0.0767 strlwr_a_bitwise_operation_cr
78856ms avg:0.0789 strlwr_a_tolower_cr
92614ms avg:0.0926 strlwr_e
104389ms avg:0.1044 strlwr_d
114527ms avg:0.1145 strlwr_g
144290ms avg:0.1443 strlwr_f
148073ms avg:0.1481 strlwr_e_cr
185303ms avg:0.1853 strlwr_g_cr
189111ms avg:0.1891 strlwr_d_cr
234481ms avg:0.2345 strlwr_f_cr
The fastest function is strlwr_c with time 22115 ms
Test with long string: string(100000, 'A')
Results:
4141ms avg:41.0000 strlwr_c_cr
7155ms avg:70.8416 strlwr_c
7935ms avg:78.5644 strlwr_b_plus1
8104ms avg:80.2376 strlwr_b_gobal_array_cr
8450ms avg:83.6634 strlwr_b_cr
8883ms avg:87.9505 strlwr_b_gobal_array
8914ms avg:88.2574 strlwr_b
52675ms avg:521.5347 strlwr_a_bitwise_operation_cr
53903ms avg:533.6931 strlwr_a_bitwise_operation
54576ms avg:540.3564 strlwr_a
60268ms avg:596.7129 strlwr_a_cr
80132ms avg:793.3861 strlwr_a_tolower
81337ms avg:805.3168 strlwr_a_tolower_cr
91819ms avg:909.0990 strlwr_e_cr
101800ms avg:1007.9208 strlwr_e
105913ms avg:1048.6436 strlwr_d_cr
107961ms avg:1068.9208 strlwr_d
115691ms avg:1145.4554 strlwr_f
121857ms avg:1206.5050 strlwr_g
126411ms avg:1251.5941 strlwr_f_cr
139462ms avg:1380.8119 strlwr_g_cr
The fastest function is strlwr_c_cr with time 4141 ms
Test with very long string: string(1000000, 'A')
Results:
4741ms avg:431.0000 strlwr_c_cr
13301ms avg:1209.1818 strlwr_b_cr
14137ms avg:1285.1818 strlwr_b_gobal_array_cr
16132ms avg:1466.5455 strlwr_c
16828ms avg:1529.8182 strlwr_b_plus1
17300ms avg:1572.7273 strlwr_b
18541ms avg:1685.5455 strlwr_b_gobal_array
57394ms avg:5217.6364 strlwr_a_bitwise_operation_cr
65325ms avg:5938.6364 strlwr_a_cr
73895ms avg:6717.7273 strlwr_a_bitwise_operation
74765ms avg:6796.8182 strlwr_a
90188ms avg:8198.9091 strlwr_a_tolower_cr
102875ms avg:9352.2727 strlwr_e_cr
105077ms avg:9552.4545 strlwr_e
112097ms avg:10190.6364 strlwr_a_tolower
115713ms avg:10519.3636 strlwr_d_cr
127222ms avg:11565.6364 strlwr_f_cr
130179ms avg:11834.4545 strlwr_g
130545ms avg:11867.7273 strlwr_f
132292ms avg:12026.5455 strlwr_d
133993ms avg:12181.1818 strlwr_g_cr
The fastest function is strlwr_c_cr with time 4741 ms
Test with super long string: string(10000000, 'A')
Not all functions will be tested, because array is too long
Results:
88778ms avg:88778.0000 strlwr_c_cr
104002ms avg:104002.0000 strlwr_c
118009ms avg:118009.0000 strlwr_b_gobal_array_cr
156843ms avg:156843.0000 strlwr_b_gobal_array
545473ms avg:545473.0000 strlwr_a
553661ms avg:553661.0000 strlwr_a_bitwise_operation_cr
556760ms avg:556760.0000 strlwr_a_bitwise_operation
626332ms avg:626332.0000 strlwr_a_cr
817736ms avg:817736.0000 strlwr_a_tolower_cr
835059ms avg:835059.0000 strlwr_a_tolower
954510ms avg:954510.0000 strlwr_e_cr
972074ms avg:972074.0000 strlwr_e
1073152ms avg:1073152.0000 strlwr_d
1133930ms avg:1133930.0000 strlwr_d_cr
1187283ms avg:1187283.0000 strlwr_f_cr
1198201ms avg:1198201.0000 strlwr_f
1198848ms avg:1198848.0000 strlwr_g
1239352ms avg:1239352.0000 strlwr_g_cr
The fastest function is strlwr_c_cr with time 88778 ms
Winner:
strlwr_a_bitwise_operation_cr: 2
strlwr_a_cr: 1
strlwr_a_tolower_cr: 2
strlwr_c: 1
strlwr_c_cr: 9
验证函数有效性
#include <bits/stdc++.h>
using namespace std;
string strlwr_a_cr(const string& input) {
string result = input;
for (size_t i = 0; i < result.size(); ++i) {
if (result[i] >= 'A' && result[i] <= 'Z') {
result[i] += 32;
}
}
return result;
}
string strlwr_a(string input) {
for (size_t i = 0; i < input.size(); ++i) {
if (input[i] >= 'A' && input[i] <= 'Z') {
input[i] += 32;
}
}
return input;
}
string strlwr_a_bitwise_operation_cr(const string& input) {
string result = input;
for (size_t i = 0; i < result.size(); ++i) {
if (result[i] >= 'A' && result[i] <= 'Z') {
result[i] |= 32;
}
}
return result;
}
string strlwr_a_bitwise_operation(string input) {
for (size_t i = 0; i < input.size(); ++i) {
if (input[i] >= 'A' && input[i] <= 'Z') {
input[i] |= 32;
}
}
return input;
}
string strlwr_a_tolower_cr(const string& input) {
string result = input;
for (size_t i = 0; i < result.size(); ++i) {
if (result[i] >= 'A' && result[i] <= 'Z') {
result[i] = tolower(result[i]);
}
}
return result;
}
string strlwr_a_tolower(string input) {
for (size_t i = 0; i < input.size(); ++i) {
if (input[i] >= 'A' && input[i] <= 'Z') {
input[i] = tolower(input[i]);
}
}
return input;
}
string strlwr_b_cr(const string& input) {
string result = input;
// +1 是为了存放字符串结束符
// 不加这个测试长字符串和空字符串会 segmentation fault
char cstr[result.size() + 1];
strcpy(cstr, result.c_str());
strlwr(cstr);
return string(cstr);
}
string strlwr_b(string str) {
char cstr[str.size()];
strcpy(cstr, str.c_str());
strlwr(cstr);
return string(cstr);
}
string strlwr_b_plus1(string str) {
// +1 是为了存放字符串结束符
char cstr[str.size() + 1];
strcpy(cstr, str.c_str());
strlwr(cstr);
return string(cstr);
}
char cstr_array[100000001];
string strlwr_b_gobal_array_cr(const string& input) {
string result = input;
// +1 是为了存放字符串结束符
// 不加这个测试长字符串和空字符串会 segmentation fault
strcpy(cstr_array, result.c_str());
strlwr(cstr_array);
return string(cstr_array);
}
string strlwr_b_gobal_array(string str) {
strcpy(cstr_array, str.c_str());
strlwr(cstr_array);
return string(cstr_array);
}
string strlwr_c_cr(const string& input) {
return string(strlwr((char*)input.c_str()));
}
string strlwr_c(string str) { return string(strlwr((char*)str.c_str())); }
string strlwr_d_cr(const string& input) {
string result = input;
std::transform(result.begin(), result.end(), result.begin(), ::tolower);
return result;
}
string strlwr_d(string str) {
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
return str;
}
string strlwr_e_cr(const string& input) {
string str = input;
std::for_each(str.begin(), str.end(), [](char& c) { c = ::tolower(c); });
return str;
}
string strlwr_e(string str) {
std::for_each(str.begin(), str.end(), [](char& c) { c = ::tolower(c); });
return str;
}
string strlwr_f_cr(const string& input) {
stringstream ss;
for (char c : input) {
ss << static_cast<char>(tolower(c));
}
return ss.str();
}
string strlwr_f(string str) {
stringstream ss;
for (char c : str) {
ss << static_cast<char>(tolower(c));
}
return ss.str();
}
string strlwr_g_cr(const string& input) {
string str = input;
for (auto it = str.begin(); it != str.end(); it++) {
*it = tolower(*it);
}
return str;
}
string strlwr_g(string str) {
for (auto it = str.begin(); it != str.end(); it++) {
*it = tolower(*it);
}
return str;
}
void solve() {
string str =
"TQuickBFJOveThLaD.0123456789"
"!@#$%^&*()_+{}:'<>?";
cout << str << setw(30) << "original" << endl;
cout << strlwr_a_cr(str) << setw(30) << "strlwr_a_cr" << endl;
cout << strlwr_a(str) << setw(30) << "strlwr_a" << endl;
cout << strlwr_a_bitwise_operation_cr(str) << setw(30)
<< "strlwr_a_bitwise_operation_cr" << endl;
cout << strlwr_a_bitwise_operation(str) << setw(30)
<< "strlwr_a_bitwise_operation" << endl;
cout << strlwr_a_tolower_cr(str) << setw(30) << "strlwr_a_tolower_cr"
<< endl;
cout << strlwr_a_tolower(str) << setw(30) << "strlwr_a_tolower" << endl;
cout << strlwr_b_cr(str) << setw(30) << "strlwr_b_cr" << endl;
cout << strlwr_b(str) << setw(30) << "strlwr_b" << endl;
cout << strlwr_b_plus1(str) << setw(30) << "strlwr_b_plus1" << endl;
cout << strlwr_b_gobal_array_cr(str) << setw(30) << "strlwr_b_gobal_array_cr" << endl;
cout << strlwr_b_gobal_array(str) << setw(30) << "strlwr_b_gobal_array" << endl;
cout << strlwr_c_cr(str) << setw(30) << "strlwr_c_cr" << endl;
cout << strlwr_c(str) << setw(30) << "strlwr_c" << endl;
cout << strlwr_d_cr(str) << setw(30) << "strlwr_d_cr" << endl;
cout << strlwr_d(str) << setw(30) << "strlwr_d" << endl;
cout << strlwr_e_cr(str) << setw(30) << "strlwr_e_cr" << endl;
cout << strlwr_e(str) << setw(30) << "strlwr_e" << endl;
cout << strlwr_f_cr(str) << setw(30) << "strlwr_f_cr" << endl;
cout << strlwr_f(str) << setw(30) << "strlwr_f" << endl;
cout << strlwr_g_cr(str) << setw(30) << "strlwr_g_cr" << endl;
cout << strlwr_g(str) << setw(30) << "strlwr_g" << endl;
cout << endl;
}
int main() { solve(); }
有效性测试结果
TQuickBFJOveThLaD.0123456789!@#$%^&*()_+{}:'<>? original
tquickbfjovethlad.0123456789!@#$%^&*()_+{}:'<>? strlwr_a_cr
tquickbfjovethlad.0123456789!@#$%^&*()_+{}:'<>? strlwr_a
tquickbfjovethlad.0123456789!@#$%^&*()_+{}:'<>? strlwr_a_bitwise_operation_cr
tquickbfjovethlad.0123456789!@#$%^&*()_+{}:'<>? strlwr_a_bitwise_operation
tquickbfjovethlad.0123456789!@#$%^&*()_+{}:'<>? strlwr_a_tolower_cr
tquickbfjovethlad.0123456789!@#$%^&*()_+{}:'<>? strlwr_a_tolower
tquickbfjovethlad.0123456789!@#$%^&*()_+{}:'<>? strlwr_b_cr
tquickbfjovethlad.0123456789!@#$%^&*()_+{}:'<>? strlwr_b
tquickbfjovethlad.0123456789!@#$%^&*()_+{}:'<>? strlwr_b_plus1
tquickbfjovethlad.0123456789!@#$%^&*()_+{}:'<>? strlwr_b_gobal_array_cr
tquickbfjovethlad.0123456789!@#$%^&*()_+{}:'<>? strlwr_b_gobal_array
tquickbfjovethlad.0123456789!@#$%^&*()_+{}:'<>? strlwr_c_cr
tquickbfjovethlad.0123456789!@#$%^&*()_+{}:'<>? strlwr_c
tquickbfjovethlad.0123456789!@#$%^&*()_+{}:'<>? strlwr_d_cr
tquickbfjovethlad.0123456789!@#$%^&*()_+{}:'<>? strlwr_d
tquickbfjovethlad.0123456789!@#$%^&*()_+{}:'<>? strlwr_e_cr
tquickbfjovethlad.0123456789!@#$%^&*()_+{}:'<>? strlwr_e
tquickbfjovethlad.0123456789!@#$%^&*()_+{}:'<>? strlwr_f_cr
tquickbfjovethlad.0123456789!@#$%^&*()_+{}:'<>? strlwr_f
tquickbfjovethlad.0123456789!@#$%^&*()_+{}:'<>? strlwr_g_cr
tquickbfjovethlad.0123456789!@#$%^&*()_+{}:'<>? strlwr_g