问题描述
我读写大code卷2,并显示以下的strlen impelementation:
I am reading "Write Great Code Volume 2" and it shows the following strlen impelementation:
int myStrlen( char *s )
{
char *start;
start = s;
while( *s != 0 )
{
++s;
}
return s - start;
}
书上说,这实现是典型的缺乏经验的C程序员。我已经用C编码在过去的11年,我不知道怎样写一个函数比这更好的在C(我能想到用汇编编写更好的东西)。这怎么可能写出code比在C更好?我看了标准库实现glibc中的strlen函数和我无法理解它的大部分。我在哪里可以找到关于如何编写高度优化的code更好的信息?
the book says that this implementation is typical for an inexperienced C programmer. I have been coding in C for the past 11 years and i can't see how to write a function better than this in C(i can think of writing better thing in assembly). How is it possible to write code better than this in C? I looked the standard library implementation of the strlen function in glibc and I couldn't understand most part of it. Where can I find better information on how to write highly optimized code?
推荐答案
的strlen (),一个由博文科尔姆·MacCarthaigh:
From Optimising strlen(), a blogpost by Colm MacCarthaigh:
不幸的是C,我们注定要为O(n)的实现,最好的情况下,但我们仍然没有这样做......我们能做点什么n的极具规模。
这使在什么方向就可以工作,加快它很好的例子。而且从它的另一个报价
It gives good example in what direction you can work to speed it up. And another quote from it
有时候会真的快只是让你真的疯了。
这篇关于如何写出更好的strlen函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!