I m using Math.ceil ( Math.abs (x )
within a loop.
Can anyone realize any optimization for this operation? (Bitwise or what?)
欢迎你在上的基准。
I m using Math.ceil ( Math.abs (x )
within a loop.
Can anyone realize any optimization for this operation? (Bitwise or what?)
欢迎你在上的基准。
Math.abs don t 更加简单地上网 Javacast Core
case MathObjectImp::Abs:
result = ( arg < 0 || arg == -0) ? (-arg) : arg;
但是,面纱使用C面纱功能
case MathObjectImp::Ceil:
result = ::ceil(arg);
so testing on JSpref
http://jsperf.com/math-ceil-vs-bitwise
bitwise is faster
testing @orangedog s answer http://jsperf.com/math-ceil-vs-bitwise/2 Math.ceil is faster
So I guess your best choice is:
var n = Math.abs(x);
var f = (n << 0),
f = f == n ? f : f + 1;
<代码>x” 0 ? Math.ceil(-x) :Math.ceil(x)
http://jsperf.com/math-ceil-math-abs-x/6”rel=“nofollow”jsPerf page 。 Ignore the "some bitwisemente”bell;以上表述并不使用任何标签。
Javascript isn t a prepared Language such C, so bitwise operations that can work questions in such instruments, aren t so high in JS, as number are Deposit as 64 bitroting Point. 查阅 SO post。
即便如此,你在联合材料中撰写的内容将在一定程度上通过原始浏览器转化为本地的法典,而根据执行情况,这可能会更快或更慢。
自Math.ceil
和Math.abs
建立以来;我猜测它们重新优化,因此,我怀疑你能通过自己做一些trick,获得更好的业绩。
底线:三个东西阻碍你更快地做到这一点:
parseInt(Math.abs(x)+ 1
,在代谢时,其速度为30%左右
由于这一论点总是正面的,Math.ceil()的分支是不必要的。
这里,不需要做任何研究:
((x >= 0 ? x : -x) + 0.5) >> 0
两种最快的计算方法(现代浏览器的速率几乎相同):
function f (n) {
return (~~n) + 1;
}
// or
function f1 (n) {
return (n | 0) + 1;
}
// some tests, ~~ operator seems to work identicaly on numbers:
( 3.3 | 0 ) === 3;
( 3.8 | 0 ) === 3;
( -3.3 | 0 ) === -3;
( -3.8 | 0 ) === -3;
与<代码>Math.底(-3.3)=Math(-3.8)== -4
How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.
I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...
Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...
Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...
I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...
Is it possible for someone to give me a few pointers on how to display a multidimensional array in the form of a bar graph? The array is multidimensional, with three elements in each part - and the ...
Is it possible to reload a form after file-input change? I have a form where the user can chose an image for upload. I also have a php script which displays that image resized. I only wonder if it ...
I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.