在JavaScript中对数字进行平方的最快方法是什么?
function squareIt(number) {
return Math.pow(number,2);
}
function squareIt(number) {
return number * number;
}
Run Code Online (Sandbox Code Playgroud)
或者其他一些我不知道的方法.我不是在寻找一个高尔夫球的答案,但平均而言,编译器的答案可能是最短的.
编辑:我看到为什么比乘以两个随机数快一个数字?这似乎表明平方比两个随机数乘以更快,并假设n*n不会利用这个,但Math.pow(n,2)会.正如jfriend00在评论中所指出的,然后在回答中,http: //jsperf.com/math-pow-vs-simple-multiplication/10 似乎表明直接乘法在除了Firefox之外的所有内容中都比较快同样很快).
我正在阅读http://daleswanson.blogspot.com/2012/07/how-to-compile-c-code-in-notepad-with.html并决定尝试,以便我可以继续在Notepad ++中编写代码并有一个较短的编译/运行周期.
当我试图将编译/运行代码输入NppExec时,它无法正常工作.我现在的代码是:
npp_save
cd "$(C:\Users\Bart\Desktop\new delete me)"
g++ "$(test.cpp)" -o $(testme.exe) -march=native -O3
NPP_RUN $(testme.exe)
Run Code Online (Sandbox Code Playgroud)
这是基于我给出的第一个链接:
npp_save
cd "$(CURRENT_DIRECTORY)"
g++ "$(FILE_NAME)" -o $(NAME_PART) -march=native -O3
NPP_RUN $(NAME_PART)
Run Code Online (Sandbox Code Playgroud)
Notepad ++在其控制台中提供了以下信息:
NPP_SAVE: C:\Users\Bart\Desktop\new delete me\test.cpp
CD:
Current directory: C:\Program Files (x86)\Notepad++
g++ "" -o -march=native -O3
CreateProcess() failed with error code 2:
The system cannot find the file specified.
NPP_RUN:
- empty command
Run Code Online (Sandbox Code Playgroud)
从其他页面看起来好像我只需要粘贴该代码,所有大写单词不是要替换而是变量.所以我使用了这段代码:
npp_save
cd "$(CURRENT_DIRECTORY)"
g++ "$(FILE_NAME)" -o $(NAME_PART) -march=native -O3
NPP_RUN $(NAME_PART)
Run Code Online (Sandbox Code Playgroud)
这在Notepad ++控制台中给出了以下内容:
NPP_SAVE: C:\Users\Bart\Desktop\new …Run Code Online (Sandbox Code Playgroud)