相关疑难解决方法(0)

为什么要快速运行glibc的问题太复杂了?

我在这里浏览strlen代码,想知道是否真的需要代码中使用的优化?例如,为什么下面这样的东西不能同样好或更好?

unsigned long strlen(char s[]) {
    unsigned long i;
    for (i = 0; s[i] != '\0'; i++)
        continue;
    return i;
}
Run Code Online (Sandbox Code Playgroud)

较简单的代码对编译器进行优化是否更好或更容易?

strlen链接后面页面上的代码如下所示:

/* Copyright (C) 1991, 1993, 1997, 2000, 2003 Free Software Foundation, Inc.
   This file is part of the GNU C Library.
   Written by Torbjorn Granlund (tege@sics.se),
   with help from Dan Sahlin (dan@sics.se);
   commentary by Jim Blandy (jimb@ai.mit.edu).

   The GNU C Library is free software; you can redistribute it and/or
   modify it under …
Run Code Online (Sandbox Code Playgroud)

c optimization portability glibc strlen

283
推荐指数
7
解决办法
5万
查看次数

魔法位如何改进 glibc 中的 strlen 函数

我正在查看glibc 的 strlen的来源。他们用来magic bits求字符串的长度。有人可以解释一下它是如何工作的吗?谢谢

c glibc strlen

5
推荐指数
1
解决办法
771
查看次数

标签 统计

c ×2

glibc ×2

strlen ×2

optimization ×1

portability ×1