Interesting timing
Posted: Tue Jul 13, 2021 5:11 pm
I have never been a fan of optimizing ahead of time. I am a large proponent
of writing modular code, so when one needs to optimize one can chose small routines to work on. Take for instance trimming white space.
These routines all trim the leading whitespace from a string. We can measure the time it takes for each way. Example code:
and the results are (in debug)
ltrim of Howdy return 'Howdy' in 328 usec
llltrim of Howdy return 'Howdy' in 1 usec
lltrim of Howdy return 'Howdy' in 4 usec
of writing modular code, so when one needs to optimize one can chose small routines to work on. Take for instance trimming white space.
These routines all trim the leading whitespace from a string. We can measure the time it takes for each way. Example code:
#include <iostream>
#include <algorithm>
#include <regex>
#include <string>
#include <ctype.h>
#include <chrono>
// Regex way to trim
std::string ltrim(const std::string& s) {
return std::regex_replace(s, std::regex("^\\s+"), std::string(""));
}
// string search and find way to trim
std::string lltrim(const std::string& s) {
auto loc = s.find_first_not_of(" \t\f\r\v\n");
if (loc == std::string::npos){
return std::string();
}
else {
return s.substr(loc) ;
}
}
// for way to trim
std::string llltrim(const std::string& s) {
auto count = 0 ;
for (auto &c : s) {
if (!isspace(c)){
return s.substr(count) ;
}
else {
count++;
}
}
return std::string();
}
int main(int argc, const char * argv[]) {
std::string test = " Howdy";
// Measure one way
auto start_time = std::chrono::steady_clock::now();
auto a = ltrim(test);
auto end = std::chrono::steady_clock::now();
auto diff = end - start_time ;
std::cout <<"ltrim of " << test<<" return '"<<a<<"' in "<< std::chrono::duration_cast<std::chrono::microseconds>(diff).count() << " usec" << std::endl;
// Measure way
start_time = std::chrono::steady_clock::now();
a = llltrim(test);
end = std::chrono::steady_clock::now();
diff = end - start_time ;
std::cout <<"llltrim of " << test<< " return '"<<a<<"' in "<< std::chrono::duration_cast<std::chrono::microseconds>(diff).count() << " usec" << std::endl;
// Measure way
start_time = std::chrono::steady_clock::now();
a = lltrim(test);
end = std::chrono::steady_clock::now();
diff = end - start_time ;
std::cout <<"lltrim of " << test<< " return '"<<a<<"' in "<< std::chrono::duration_cast<std::chrono::microseconds>(diff).count() << " usec" << std::endl;
}
#include <algorithm>
#include <regex>
#include <string>
#include <ctype.h>
#include <chrono>
// Regex way to trim
std::string ltrim(const std::string& s) {
return std::regex_replace(s, std::regex("^\\s+"), std::string(""));
}
// string search and find way to trim
std::string lltrim(const std::string& s) {
auto loc = s.find_first_not_of(" \t\f\r\v\n");
if (loc == std::string::npos){
return std::string();
}
else {
return s.substr(loc) ;
}
}
// for way to trim
std::string llltrim(const std::string& s) {
auto count = 0 ;
for (auto &c : s) {
if (!isspace(c)){
return s.substr(count) ;
}
else {
count++;
}
}
return std::string();
}
int main(int argc, const char * argv[]) {
std::string test = " Howdy";
// Measure one way
auto start_time = std::chrono::steady_clock::now();
auto a = ltrim(test);
auto end = std::chrono::steady_clock::now();
auto diff = end - start_time ;
std::cout <<"ltrim of " << test<<" return '"<<a<<"' in "<< std::chrono::duration_cast<std::chrono::microseconds>(diff).count() << " usec" << std::endl;
// Measure way
start_time = std::chrono::steady_clock::now();
a = llltrim(test);
end = std::chrono::steady_clock::now();
diff = end - start_time ;
std::cout <<"llltrim of " << test<< " return '"<<a<<"' in "<< std::chrono::duration_cast<std::chrono::microseconds>(diff).count() << " usec" << std::endl;
// Measure way
start_time = std::chrono::steady_clock::now();
a = lltrim(test);
end = std::chrono::steady_clock::now();
diff = end - start_time ;
std::cout <<"lltrim of " << test<< " return '"<<a<<"' in "<< std::chrono::duration_cast<std::chrono::microseconds>(diff).count() << " usec" << std::endl;
}
ltrim of Howdy return 'Howdy' in 328 usec
llltrim of Howdy return 'Howdy' in 1 usec
lltrim of Howdy return 'Howdy' in 4 usec