#include <iostream> #include <iomanip> #include <vector> #include <sstream> #include <chrono> class ToHexIterator : public std::iterator<std::input_iterator_tag, int>{ char* it_; char* end_; int current_; bool isHex(const char c){ return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'); } char toUpperCase(const char c){ if (c >= 'a' && c <= 'f'){ return (c - 'a') + 'A'; } return c; } int toNibble(const char c){ auto x = toUpperCase(c); if (x >= '0' && x <= '9'){ return x - '0'; } else { return (x - 'A') + 10; } } public: ToHexIterator() :it_{ nullptr }, end_{ nullptr }, current_{}{} //default constructed means end iterator ToHexIterator(char* begin, char* end) :it_{ begin }, end_{ end }, current_{}{ while (!isHex(*it_) && it_ != end_){ ++it_; }; //make sure we are pointing to valid stuff ++(*this); } bool operator==(const ToHexIterator &other){ return it_ == nullptr && end_ == nullptr && other.it_ == nullptr && other.end_ == nullptr; } bool operator!=(const ToHexIterator &other){ return !(*this == other); } int operator*(){ return current_; } ToHexIterator & operator++(){ current_ = 0; if (it_ != end_) { while (isHex(*it_) && it_ != end_){ current_ <<= 4; current_ += toNibble(*it_); ++it_; }; while (!isHex(*it_) && it_ != end_){ ++it_; }; } else { it_ = nullptr; end_ = nullptr; } return *this; } ToHexIterator operator++(int){ ToHexIterator temp(*this); ++(*this); return temp; } }; void __attribute__ ((noinline)) test1(std::vector<int> &v){ char in[] = "1,3,8,b,e,ff,10"; std::copy(ToHexIterator{ std::begin(in), std::end(in) }, ToHexIterator{}, std::back_inserter(v)); } void __attribute__ ((noinline)) test2(std::vector<int> &v){ std::istringstream iss("1,3,8,b,e,ff,10"); unsigned int num = 0; while(iss >> std::hex >> num || !iss.eof()) { if(iss.fail()) { iss.clear(); char dummy; iss >> dummy; continue; } if(num <= 0xff) { v.push_back(num); } else { // Error single byte value expected } } } int main() { std::vector<int> dataValues; std::vector<int> dataValues2; std::chrono::nanoseconds time[2]{{},{}}; for(int i=0;i<100;i++){ const auto t1 = std::chrono::high_resolution_clock::now(); test2(dataValues2); const auto t2 = std::chrono::high_resolution_clock::now(); time[0] += t2 - t1; const auto t3 = std::chrono::high_resolution_clock::now(); test1(dataValues); const auto t4 = std::chrono::high_resolution_clock::now(); time[1] += t4 - t3; } typedef std::chrono::nanoseconds output_time; const char* const out_unit = " ns"; std::cout << "1: " << std::chrono::duration_cast<output_time>(time[0]).count() << out_unit << "\n"; std::cout << "2: " << std::chrono::duration_cast<output_time>(time[1]).count() << out_unit << "\n"; std::cout << std::hex << std::setfill('0'); for(int i:dataValues) { std::cout << "0x" << std::setw(2) << static_cast<unsigned int>(i) << std::endl; } for(int i:dataValues2) { std::cout << "0x" << std::setw(2) << static_cast<unsigned int>(i) << std::endl; } return 0; }
Standard input is empty
1: 452860 ns 2: 45095 ns 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10 0x01 0x03 0x08 0x0b 0x0e 0xff 0x10