- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在编写一个例程以在嵌入式 (ARM Cortex M0 @16MHz) 应用程序的指定内存块中查找字符串,我想知道为什么我编写的两个不同版本以不同的速度运行。
char* memstr(char* mem, uint32_t n, char* str) {
if( (str[0] == '\0') || (n == 0) ) return NULL;
uint32_t i = 0;
char* max_mem;
max_mem = mem + n;
while( mem < max_mem ) {
if( *mem != str[i] ) {
mem -= i;
i = 0;
} else {
if(str[i+1] == '\0') return mem - i;
i++;
}
mem++;
}
return NULL;
}
char* memstr2(char* mem, uint32_t n, char* str) {
if( (str[0] == '\0') || (n == 0) ) return NULL;
uint32_t c = 0;
uint32_t i = 0;
while( c < n ) {
if( mem[c] != str[i] ) {
c -= i;
i = 0;
} else {
i++;
if(str[i] == '\0') return &mem[c - i + 1];
}
c++;
}
return NULL;
}
在 20 到 200 字节的内存中查找 7 个字符的字符串时,memstr 始终比 memstr2 快 1us。例如在 110 字节中查找 7 个字符的字符串,memstr 需要 106us,memstr2 需要 107us。 1us 听起来可能没什么大不了的,但在每个滴答声都很重要的嵌入式应用程序中,这是一个缺点。
一个额外的问题:这也促使我编写自己的 strstr,它比标准 strstr 更快(例如,在 207 个字符串中找到一个 7 个字符的字符串需要 my_strstr 236us 和 strstr 274us)。这有什么问题,因为 strstr 必须非常优化?
char* my_strstr(char* str1, char* str2) {
uint32_t i = 0;
if( str2[0] == '\0' ) return NULL;
while( *str1 != '\0' ) {
if( *str1 != str2[i] ) {
str1 -= i;
i = 0;
} else {
i++;
if(str2[i] == '\0') return (str1 - i - 1);
}
str1++;
}
return NULL;
}
最佳答案
首先,如果您搜索以两个相同字符开头的字符串,这两个函数都不起作用:如果您搜索 xxabcde 并且该字符串包含 xxxabcde,那么当您注意到 xxabcde 的 a 与第三个 x 不匹配时,您已经跳过了两个 x,因此不会匹配该字符串。
您也不检查是否搜索空字符串,在这种情况下您的代码会产生未定义的行为。
你用内存来比较内存。但是仅仅将内存与单个字符进行比较,你就可以做大量的工作。如果您搜索“abcde”,首先您必须找到字母 a。所以我会先检查一个空字符串,然后读取第一个字符。然后首先循环检查该字符。
char first = str2 [0];
if (first == '\0') return mem;
for (; mem < maxmem; ++mem) if (*mem == first) {
... check whether there is a match
}
你应该检查你的数据。如果您希望搜索字符串早点出现与您希望它通常根本不存在,您将编写不同的代码。
关于自定义 memstr (strstr) 速度优化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38405127/
题目地址:https://leetcode.com/problems/implement-strstr/description/ 题目描述 Implement strStr(). Return
这里是 C 的初学者。 我知道 strstr() 可用于查找字符串是否包含某个子字符串并且 printf() 可以显示彩色输出(如此处解释:stdlib and colored output in C
我有一个二进制数据文件,其中散布着各种字符串。我正在尝试编写 C 代码来查找文件中第一次出现的用户指定字符串。 (我知道这可以用 bash 完成,但出于其他原因我需要一个 C 代码。)目前的代码是:
我制作了 strstr() 函数,但程序没有给出任何输出,只是一个空白屏幕。请查看代码。 #include #include const char* mystrstr(const char *
你能找出这段代码中的错误吗,即使我传递了有效的参数来查找,它也无法打印歌曲 #include #include char tracks[][80] = { "I left my heart
我正在尝试匹配整个字符串,而不仅仅是其中的一部分。例如,如果 needle 是 2,我只想匹配 string 2 而不是 20、02 或 22 或任何相关内容。 我正在使用 strstr 作为: #i
在 OpenGL superbible 4th ed 中,第 70 页上的示例读取 //returns space-delimited names of all extensions supporte
你能找出这段代码中的错误吗,即使我传递了有效的参数来查找,它也无法打印歌曲 #include #include char tracks[][80] = { "I left my heart
这里的目标是将整个文本文件转储到缓冲区中,然后使用 strcasestr() 函数查找我在缓冲区中查找的单词的指针。它不断地给我段错误错误。起初,我认为可能是尺寸问题,所以我尝试使用较小的尺寸,但也不
我试图通过 strstr() 函数检查子字符串(“DATA”)是否(以及在哪里)位于大字符串(位于缓冲区 - LinearBuffer 中),但它似乎不起作用,并且我不知道为什么即使我的源字符串(位于
这是我到目前为止编写的代码,但我想用 int myStrStr 而不是 char myStrStr 但是当我用 int 代替时的 char ,它给了我一个错误。请帮忙。 int my_strlen(c
如果进程名称是“System”,则不进行 DbgPrint。如果是任何其他进程,现在 DbgPrint 都可以,但由于某种原因,它没有像预期的那样工作。 我尝试过:if( strstr( ImageN
我正在尝试使用 strstr 使用子字符串搜索任何匹配项并将其与文本行进行比较,但到目前为止尚未成功获得匹配项。我正在使用 popen 打开并读取文件,同时尝试仅使用 ipv4 地址进行搜索以查找匹配
我现在编写的程序遇到了一些问题。 strstr 仅当子字符串位于字符串末尾时才输出 此后它还会输出一些垃圾字符 我在使用“const char *haystack”然后向其中添加输入时遇到了问题,因此
我正在从头开始学习 C,我尝试制作一个小程序,在二维数组中搜索用户输入。 但是我的代码没有按预期工作并且总是返回 false。 我的代码: #include #include char songs[]
我正在从头开始学习 C,我尝试制作一个小程序,在二维数组中搜索用户输入。 但是我的代码没有按预期工作并且总是返回 false。 我的代码: #include #include char songs[]
由于 strstr 函数,我在该方法内有一个无限循环。是因为我将结构类型与字符类型相匹配吗? car* find(char* type){ car* stringCurr = list_hea
我的目标是在名为 myStrStr 的 C 函数中重新创建 strstr,如果在 haystack 中找到子字符串并且返回 1,则返回 1 0 如果不是,则将匹配的子字符串复制到缓冲区中。 我尝试编写
所以我尝试制作自己的 strstr 函数,并实现以下功能: char *mystrstr(char *haystack, char *needle); // find the first oc
我正在阅读 String searching algorithm维基百科文章,它让我想知道什么算法 strstr在 Visual Studio 中使用?我应该尝试使用其他实现方式,还是 strstr
我是一名优秀的程序员,十分优秀!