我正在InterviewBit上尝试关于数组的问题.在这个问题中,我创建了一个内联函数,返回整数的绝对值.但有人告诉我,我的算法在提交时效率不高.但当我改为使用abs()C++库时,它给出了正确的答案判决.
这是我的功能得到了一个低效的判决 -
inline int abs(int x){return x>0 ? x : -x;}
int Solution::coverPoints(vector<int> &X, vector<int> &Y) {
int l = X.size();
int i = 0;
int ans = 0;
while (i<l-1){
ans = ans + max(abs(X[i]-X[i+1]), abs(Y[i]-Y[i+1]));
i++;
}
return ans;
}
Run Code Online (Sandbox Code Playgroud)
这是得到正确答案的那个-
int Solution::coverPoints(vector<int> &X, vector<int> &Y) {
int l = X.size();
int i = 0;
int ans = 0;
while (i<l-1){
ans = ans + max(abs(X[i]-X[i+1]), abs(Y[i]-Y[i+1])); …Run Code Online (Sandbox Code Playgroud) 为了在运行 Windows 7 pro 的英特尔核心 2 上执行一些 cmov 指令,我编写了下面的代码。它所做的只是从控制台获取一个字符串作为输入,应用一些移位操作来生成一个随机种子,然后将该种子传递给 srand,以生成一个小的伪随机数数组。然后评估伪随机数是否满足谓词函数(更任意的 bitshuffling ),并输出“*”或“_”。实验的目的是生成 cmov 指令,但是正如您在下面的反汇编中看到的那样,没有。
关于如何更改代码或 cflags 以便生成它们的任何提示?
#include <iostream>
#include <algorithm>
#include <string>
#include <cstdlib>
bool blackBoxPredicate( const unsigned int& ubref ) {
return ((ubref << 6) ^ (ubref >> 2) ^ (~ubref << 2)) % 15 == 0;
}
int main() {
const unsigned int NUM_RINTS = 32;
unsigned int randomSeed = 1;
unsigned int popCount = 0;
unsigned int * rintArray = new unsigned int[NUM_RINTS];
std::string userString;
std::cout …Run Code Online (Sandbox Code Playgroud) 我在某处看到 GCC 编译器有时更喜欢在将我的代码转换为 ASM 时不使用条件 mov。
在什么情况下它可能会选择做条件 mov 以外的事情?