Describe
Extract date from a DXF file and write it to a txt file.
Test File
A DXF file has 1422390 lines and a size of 10.5M.
Result
Replication Times: 100
Average run time before optimization: 1.3329s
Average run time after optimization: 0.6027s
Performance Improvement: (1.3329 - 0.6027) / 1.3329 * 100% = 54.8%
Debugging
Tool: Visual Studio 2019 - CPU Usage - Call Tree
Version: C++11
Mode: RelWithDebug
Solution
Find the expensive cost function in call tree
1. readDouble()
- Describe: Read a string and convert it to double.
- Analysis: Converting std::string -> std::istringstream -> double is too expensive. A simple an direct way is to use std::stod function to convert std::string to double. And a faster way to convert *const char** to float is to use atof. The double precision is unnecessary as the render precision is set to float.
-
Before Opt: 663/1350(ms) = 49.1%
- After Opt: 203/862(ms) = 23.55%
2. pointToValue()
- Describe: Converting double to std::string
- Analysis: The most expensive part is
temp = std::to_string(x) + ","
which involves two string allocations and a copy from the previous string to the new allocation. To reduce time, I replace std::to_string() to sprintf_s(). And std::to_chars(C++17) has better performent than sprintf_s(), but c++17 is not supported by our application. -
Before Opt: 218/862(ms) = 26.49%
- After Opt: 141/726(ms) = 19.42%
3. setBasic()
- Describe: Converting double to std::string and ASCII to std::string.
-
Analysis: Using std::stringstream to convert double to std::string is expensive, I use the sprintf_s function mentioned previously. Then, the ASCII value of a char is convrt to int and then to std::string. This is a blog compares different ways to convert int to std::string.
-
Before Opt: 94/726(ms) = 12.95%
- After Opt: 17/659(ms) = 2.58%
4. getHandleString()
- Describe: Converting hexadecimal number std::string to a decimal int.
-
Analysis: It is redundant that convert std::string to std::isstringstream and then to hexadecimal integer. The std::stoi can direct convert type and number system.
-
Before Opt: 43/659(ms) = 6.53%
- After Opt: 1/627(ms) = 0.16%
Difference
- Describe: There are some differences between previous output file and new file.
References
The code adapted from LibreCAD.