在没有CFStringCreateWithFormat的情况下将int转换为CFString [英] Convert int to CFString without CFStringCreateWithFormat

查看:36
本文介绍了在没有CFStringCreateWithFormat的情况下将int转换为CFString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我所需要的,非常慢.

The following is extremely slow for what I need.

CFStringCreateWithFormat(NULL, NULL, CFSTR("%d"), i);

目前,在我的测试中,这需要20,000ns才能在3gs上执行.也许听起来很快,但是我可以在执行时创建和释放两个NSMutableDictionaries.我的C很弱,但是我必须在IOS上使用等同于 itoa 的东西.

Currently this takes 20,000ns in my tests to execute on my 3gs. Perhaps that sounds fast, but I can create and release two NSMutableDictionaries in the time this executes. My C is weak, but there must be something equivalent to itoa that I can use on IOS.

推荐答案

这是我可以获得的更快的速度:

This is the faster I can get:

CFStringRef TECFStringCreateWithInteger(NSInteger integer)
{
    size_t size = 21; // long enough for 64 bits integer
    char buffer[size];
    char *characters = buffer + size;

    *(--characters) = 0; // NULL-terminated string
    int sign = integer < 0 ? -1 : 1;

    do {
        *(--characters) = '0' + (integer % 10) * sign;
        integer /= 10;
    }
    while ( integer );

    if ( sign == -1 )
        *(--characters) = '-';

    return CFStringCreateWithCString(NULL, characters, kCFStringEncodingASCII);
}

这篇关于在没有CFStringCreateWithFormat的情况下将int转换为CFString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆