Tóm tắt ngắn gọn - 123win+club

game 3c đổi thưởng 2019

Phương pháp nhanh chóng chuyển chữ hoa thành chữ thường trong C++

Chuyển sang kết quả thử nghiệm: Kết quả thử nghiệm
Kết luận:

  1. Nếu không bật tối ưu hóa, sử dụng đoạn mã sau để thực hiện chuyển đổi:
1
2
3
string strlwr_c_cr(const string& input) {
  return string(strlwr((char*)input.c_str()));
}
  1. Nếu bật tối ưu hóa, sử dụng đoạn mã sau để thực hiện chuyển đổi:
1
2
3
4
5
6
7
8
9
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;
}
  1. Tham số const string& str thường nhanh hơn so với string str.

Mã thử nghiệm

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#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;
}

// Các hàm khác được giữ nguyên...

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()});
}

// Các hàm khác và thử nghiệm được giữ nguyên...

int main() {
  cout << "Thử nghiệm với chuỗi rỗng" << endl;
  test_func_list("", 1000001);
  cout << "Thử nghiệm với ký tự đơn lẻ: H" << endl;
  test_func_list("H", 1000001);
  cout << "Thử nghiệm với hai ký tự: AH" << endl;
  test_func_list("AH", 1000001);
  // Tiếp tục các thử nghiệm khác...
}

Kết quả thử nghiệm

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Thử nghiệm với chuỗi rỗng
Kết quả:
  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
Hàm nhanh nhất là strlwr_a_cr với thời gian 2178 ms

// Các kết quả khác được giữ nguyên...

Kiểm tra hiệu lực của các hàm

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#include <bits/stdc++.h>
using namespace std;

// Định nghĩa các hàm đã liệt kê trước đó...

void solve() {
  string str =
    "TQuickBFJOveThLaD.0123456789"
    "!@#$%^&*()_+{}:'<>?";
  cout << str << setw(30) << "original" << endl;
  cout << strlwr_a_cr(str) << setw(30) << "strlwr_a_cr" << endl;
  // Các hàm khác được kiểm tra tương tự...
}

int main() { solve(); }

Kết quả kiểm tra hiệu lực

1
2
3
4
TQuickBFJOveThLaD.0123456789!@#$%^&*()_+{}:'<>?           original
tquickbfjovethlad.0123456789!@#$%^&*()_+{}:'<>?          strlwr_a_cr
tquickbfjovethlad.0123456789!@#$%^&*()_+{}:'<>?           strlwr_a
// Các kết quả khác được hiển thị tương tự...

Kết luận

Các phương pháp đã được thử nghiệm và xác minh đều hoạt động chính xác. Hàm strlwr_a_cr thường là lựa chọn nhanh nhất khi tối ưu hóa được bật.

Licensed under CC BY-NC-SA 4.0
Built with Hugo
Theme Stack thiết kế bởi Jimmy