Integer to string optimized function?

Vincent

I have currently this function to convert an unsigned integer to a string (I need a function that works on non-standard types like __uint128_t):

#include <iostream>
#include <algorithm>
#include <string>

template <typename UnsignedIntegral>
std::string stringify(UnsignedIntegral number, const unsigned int base)
{
    static const char zero = '0';
    static const char alpha = 'A';
    static const char ten = 10;
    std::string result;
    char remainder = 0;
    do {
        remainder = number%base;
        result += (remainder < ten) ? (zero+remainder) : (alpha+remainder-ten);
        number /= base;
    } while (number > 0);
    std::reverse(std::begin(result), std::end(result));
    return result;
}

int main()
{
   std::cout<<stringify(126349823, 2)<<std::endl;
   return 0;
}

Is there any way to optimize this code ?

Sebastian Redl

You may want to read this article by Alexei Alexandrescu, where he talks about low-level optimizations by using a (fixed-radix) int to string conversion as an example:

https://www.facebook.com/notes/facebook-engineering/three-optimization-tips-for-c/10151361643253920

Keep in mind that the most important thing when optimizing is always profiling.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Integer to string optimized function?

From Dev

Return integer in function String

From Dev

Select worksheet as string or integer in function

From Dev

racket to-string function for integer and string

From Dev

An optimized implementation of the Heaviside function

From Dev

How can this function be optimized

From Dev

An optimized implementation of the Heaviside function

From Dev

Why is this string to integer conversion function off by 1?

From Dev

Recursive function to convert string to integer in C

From Dev

A function that takes string parameter and returns integer pointer

From Dev

Will integer zero template parameter be optimized out?

From Dev

RxJava or F# function [List<String> to Map<Integer, String>] where Integer is the position where String changes

From Dev

Duplicate code optimized using a function

From Dev

F# Function where given a string and an integer it returns a char at position(integer) from the string

From Dev

Integer executing as a string and not an integer?

From Dev

Python 3 int() function is not converting input string to integer

From Dev

How to concat integer and string inside recursive function in C

From Dev

What is the difference when a integer, string and array pass as function parameter in javascript

From Dev

How should I treat return value of command or function? As string or integer?

From Dev

Macro in C to call a function returning integer and then return a string

From Dev

Convert integer to string with C++ compatible function for Matlab Coder

From Dev

Transform List<List<String>> into List<Integer> with Guava's Function

From Dev

What is the difference when a integer, string and array pass as function parameter in javascript

From Dev

After converting string array into integer array, the function is returning a null array

From Dev

My php function doesn't work - integer to string conversion issues

From Dev

Caesar-cypher: ord() function receives a string, says it receives an integer

From Dev

How would you pass a string to a function and return an integer?

From Dev

Python Turtle: How to use the write function to join an integer and a string with the integer coming from a list

From Dev

Optimized method to check if an integer lies in the given range/bounds check

Related Related

  1. 1

    Integer to string optimized function?

  2. 2

    Return integer in function String

  3. 3

    Select worksheet as string or integer in function

  4. 4

    racket to-string function for integer and string

  5. 5

    An optimized implementation of the Heaviside function

  6. 6

    How can this function be optimized

  7. 7

    An optimized implementation of the Heaviside function

  8. 8

    Why is this string to integer conversion function off by 1?

  9. 9

    Recursive function to convert string to integer in C

  10. 10

    A function that takes string parameter and returns integer pointer

  11. 11

    Will integer zero template parameter be optimized out?

  12. 12

    RxJava or F# function [List<String> to Map<Integer, String>] where Integer is the position where String changes

  13. 13

    Duplicate code optimized using a function

  14. 14

    F# Function where given a string and an integer it returns a char at position(integer) from the string

  15. 15

    Integer executing as a string and not an integer?

  16. 16

    Python 3 int() function is not converting input string to integer

  17. 17

    How to concat integer and string inside recursive function in C

  18. 18

    What is the difference when a integer, string and array pass as function parameter in javascript

  19. 19

    How should I treat return value of command or function? As string or integer?

  20. 20

    Macro in C to call a function returning integer and then return a string

  21. 21

    Convert integer to string with C++ compatible function for Matlab Coder

  22. 22

    Transform List<List<String>> into List<Integer> with Guava's Function

  23. 23

    What is the difference when a integer, string and array pass as function parameter in javascript

  24. 24

    After converting string array into integer array, the function is returning a null array

  25. 25

    My php function doesn't work - integer to string conversion issues

  26. 26

    Caesar-cypher: ord() function receives a string, says it receives an integer

  27. 27

    How would you pass a string to a function and return an integer?

  28. 28

    Python Turtle: How to use the write function to join an integer and a string with the integer coming from a list

  29. 29

    Optimized method to check if an integer lies in the given range/bounds check

HotTag

Archive