gpt4 book ai didi

c - memcmp 但需要将 block 与固定值进行比较

转载 作者:太空狗 更新时间:2023-10-29 15:04:16 29 4
gpt4 key购买 nike

我需要将一 block 内存与 C 中的固定值进行比较。我可以使用 memcmp 来完成吗?像这样的东西:

memcmp (starting_address , fixed_value , num_byte)

我需要 fixed_value 是一个固定值而不是 block 的起始地址。

  1. 将固定值写入整个临时内存块不是一种选择,因为我的空间有限。
  2. 使用循环逐个写入和检查内存不是一种选择,因为它非常慢。

如果不可能,谁能告诉我一个与 memcmp 一样快(或更快)的解决方案?

谢谢,

编辑:假设我有 5GB 的内存,其中包含 0。我正在努力确保它们都是 0。检查 block 的第一个字节是否安全然后执行此操作:

memcmp(起始地址,起始地址 + ONE_BYTE,FIVE_GB); ?

编辑:这就是为什么我需要使用 memcmp 而不是用户定义的循环:

此代码运行了 546 个时钟滴答:

memset(0x80000000 , 0x1 , 0x10000000);
memset(0x90000000 , 0x1 , 0x10000000);
memcmp(0x80000000 , 0x90000000 , 0x10000000);

与这个花费了 7669 个时钟滴答的对比:

unsigned int i;
int flag = 0;
int *p = 0x80000000;
int *q = 0x90000000;
while(p < 0x90000000)
{
if(*p++ != *q++)
{
flag = 1;
}
}

最佳答案

我刚刚在我的 Mac 上测试了这个循环,它胜过 memcmp:

uint64_t *p = (uint64_t *)buffer1;
uint64_t compare;
memset(&compare, 1, sizeof compare);
for (i = 0; i < length/sizeof compare; i++)
{
if (p[i] != compare)
break;
}

完整示例代码:

#include <stdio.h>
#include <string.h>
#include <sys/resource.h>
#include <time.h>
#include <stdlib.h>
#include <stdint.h>

// from: http://www.gnu.org/software/libc/manual/html_node/Elapsed-Time.html
void timeval_subtract(struct timeval *result, struct timeval *x, struct timeval *y)
{
/* Perform the carry for the later subtraction by updating y. */
if (x->tv_usec < y->tv_usec)
{
int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
y->tv_usec -= 1000000 * nsec;
y->tv_sec += nsec;
}

if (x->tv_usec - y->tv_usec > 1000000)
{
int nsec = (x->tv_usec - y->tv_usec) / 1000000;
y->tv_usec += 1000000 * nsec;
y->tv_sec -= nsec;
}

/* Compute the time remaining to wait. tv_usec is certainly positive. */
result->tv_sec = x->tv_sec - y->tv_sec;
result->tv_usec = x->tv_usec - y->tv_usec;
}

int main(int argc, char **argv)
{
struct rusage before;
struct rusage after;
struct timeval diff;
size_t i;

size_t length = strtoull(argv[1], NULL, 0);

char *buffer1 = malloc(length);
char *buffer2 = malloc(length);

printf("filling...");
fflush(stdout);
memset(buffer1, 1, length);
memset(buffer2, 1, length);
printf(" done\n");

getrusage(RUSAGE_SELF, &before);
uint64_t *p = (uint64_t *)buffer1;
uint64_t compare;
memset(&compare, 1, sizeof compare);
for (i = 0; i < length/sizeof compare; i++)
{
if (p[i] != compare)
break;
}
if (i == length/sizeof compare)
i = 0;
getrusage(RUSAGE_SELF, &after);

printf("\nloop (returned %zu):\n", i);
timeval_subtract(&diff, &after.ru_utime, &before.ru_utime);
printf("User: %ld.%06d s\n", diff.tv_sec, diff.tv_usec);

timeval_subtract(&diff, &after.ru_stime, &before.ru_stime);
printf("System: %ld.%06d s\n", diff.tv_sec, diff.tv_usec);

getrusage(RUSAGE_SELF, &before);
i = memcmp(buffer1, buffer2, length);
getrusage(RUSAGE_SELF, &after);

printf("\nmemcmp (returned %zu):\n", i);
timeval_subtract(&diff, &after.ru_utime, &before.ru_utime);
printf("User: %ld.%06d s\n", diff.tv_sec, diff.tv_usec);

timeval_subtract(&diff, &after.ru_stime, &before.ru_stime);
printf("System: %ld.%06d s\n", diff.tv_sec, diff.tv_usec);

return 0;
}

并运行结果:

$ make
clang -Wall -Wextra -Werror -O3 -g -o example example.c
./example 0x10000000
filling... done

loop (returned 0):
User: 0.024078 s
System: 0.000011 s

memcmp (returned 0):
User: 0.036752 s
System: 0.000017 s

也许你可以做类似的事情?

注意:对于那些担心缓存变暖的人,我也在循环之前尝试了 memcmp 并得到了相同的结果。

关于c - memcmp 但需要将 block 与固定值进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17033939/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com