我在这里浏览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
链接后面页面上的代码如下所示:
Run Code Online (Sandbox Code Playgroud)/* 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 …
是否__attribute__((always_inline))
强制函数由gcc内联?
我有两个 64 位整数x
和y
. 每个代表5个无符号短整数:前10位代表第一个整数,接下来的13位代表第二个整数,接下来的16位代表第三个整数,接下来的14位代表第四个整数,其余位代表第 5 个整数。
设x0
,x1
,x2
,x3
,x4
是构成5个短整型x
。设y0
,y1
,y2
,y3
,y4
是构成5个短整型y
。我需要知道是否x0 < y0
AND x1 < y1
AND x2 < y2
AND x3 < y3
AND x4 < y4
。
我认为最简单的解决方案是转移:
bool allLess(std::size_t x, std::size_t y)
{
if(x >= y) return 0;
int shift[] = {10, 13, 16, 14};
for(int i = 0; i < …
Run Code Online (Sandbox Code Playgroud)