Fast string to integer

Fast string to integer#

Problem#

In typical cases, strtoul( ) is used to convert strings in decimal or hexadecimal to integers.
However, if strtoul( ) is used repeatedly many times, the processing speed becomes slow.
The following code was repeated 300,000,000 times with strtoul( ), and it took about 5 seconds.

#include <iostream>
#include <chrono>

int main()
{
	const char* hexString = "19AF";   // 0x19AF = 6575

	auto start = std::chrono::high_resolution_clock::now();

	for (int i = 0; i < 300000000; i++)
	{
		int value = strtoul(hexString, NULL, 16);
	}

	auto end = std::chrono::high_resolution_clock::now();
	std::chrono::duration<double, std::milli> elapsed = end - start;
	std::cout << "time: " << elapsed.count() << " ms" << std::endl;

    return 0;
}

Improvement#

Using a LookUp Table (LUT) can process faster than strtoul( ).
The characters included in decimal and hexadecimal strings are as follows.