std::atoll with VC++
Asked Answered
B

5

10

I have been using std::atoll from cstdlib to convert a string to an int64_t with gcc. That function does not seem to be available on the Windows toolchain (using Visual Studio Express 2010). What is the best alternative?

I am also interested in converting strings to uint64_t. Integer definitions taken from cstdint.

Bombe answered 7/7, 2011 at 12:25 Comment(1)
it seems this problem is fixed in VS2013 connect.microsoft.com/VisualStudio/feedback/details/752386/…Sweatshop
C
8

MSVC have _atoi64 and similar functions, see here

For unsigned 64 bit types, see _strtoui64

Chaqueta answered 7/7, 2011 at 13:1 Comment(2)
For anyone else, as a reference, this doesn't seem to have an equivalent for uint64_t, I switched to working with int64_t (casting it from a 3rd party library)Bombe
@Cookie, added link to similar functions for unsigned 64 bit typesChaqueta
M
5
  • use stringstreams (<sstream>)

    std::string numStr = "12344444423223";
    std::istringstream iss(numStr);
    long long num;
    iss>>num;
    
  • use boost lexical_cast (boost/lexical_cast.hpp)

     std::string numStr = "12344444423223";
     long long num = boost::lexical_cast<long long>(numStr);
    
Maeve answered 7/7, 2011 at 12:27 Comment(5)
@Cookie: You have run a performance test and found that converting a string to a number is your bottleneck?Maeve
valgrind is telling me that e.g. strtod is 8% of total time. atol is a bit behind with 4%. I am actually rewriting strtod to get rid of those 8%. stringstreams are considerably slower than atol. and lexical_cast is the slowest. Most of the time is spent parsing some 100 meg csv.Bombe
The relative costs are not important if the absolute costs are negligible. Does your script run long and/or often enough to justify optimizations? (for learning purposes is of course okay)Marge
It runs often overnight, and it often runs over 24 hours, if often runs on all 8 cores non-stop, and I often have to wait for it. A 20% difference in performance is very noticeable. Is this really the point here? Is it that hard to accept that I care about performance? Or would you like to dissect my whole project and approach before I am worthy of an answer?Bombe
You do not know then whether strtod() has the same performance on VC++? std::sscanf() might be worth profiling in comparison to std::istringstream.Hovis
M
2

If you have run a performance test and concluded that the conversion is your bottleneck and should be done really fast, and there's no ready function, I suggest you write your own. here's a sample that works really fast but has no error checking and deals with only positive numbers.

long long convert(const char* s)
{
    long long ret = 0;
    while(s != NULL)
    {
       ret*=10; //you can get perverted and write ret = (ret << 3) + (ret << 1) 
       ret += *s++ - '0';
    }
    return ret;
}
Maeve answered 7/7, 2011 at 12:54 Comment(1)
Thanks Armen, I just might. It is very similar to #5831368 for doubles.Bombe
H
1

Do you have strtoull available in your <cstdlib>? It's C99. And C++0x should also have stoull to work directly on strings.

Homunculus answered 7/7, 2011 at 12:56 Comment(1)
Sadly, MSVC does not support C99.Chaqueta
G
1

Visual Studio 2013 finally has std::atoll.

Gurge answered 25/9, 2014 at 9:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.