高效的记忆体 [英] Efficient memcspn

查看:88
本文介绍了高效的记忆体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道memcspn函数的有效实现吗?它的行为应类似于strcspn,但要在内存缓冲区而不是在以null结尾的字符串中查找范围.目标编译器是visualC ++.

Does anyone know of an efficient implementation of a memcspn function?? It should behave like strcspn but look for the span in a memory buffer and not in a null terminated string. The target compiler is visualC++ .

谢谢, 卢卡

推荐答案

一种近乎最佳的实现:

size_t memcspan(const unsigned char *buf, size_t len, const unsigned char *set, size_t n)
{
    size_t i;
    char set2[1<<CHAR_BIT] = {0};
    while (n--) set2[set[n]] = 1;
    for (i=0; i<len && !set2[buf[i]]; i++);
    return i;
}

对于set2,使用位数组而不是字节数组可能更好,这取决于算术或更多的缓存颠簸在您的计算机上是否更昂贵.

It might be better to use a bit array instead of a byte array for set2, depending on whether arithmetic or a little bit more cache thrashing is more expensive on your machine.

这篇关于高效的记忆体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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