Which is better to use `Int((n+1)/2)`, `round(Int, (n+1)/2)` or `Int((n+1)//2)`?
I have a odd number n
and want to use (n+1)/2
as an array index. What is the best way to calculate the index? I just came up with to use Int((n+1)/2)
, round(Int, (n+1)/2))
and Int((n+1)//2)
. Which is better or don't I need to too worry about them?
julia
add a comment |
I have a odd number n
and want to use (n+1)/2
as an array index. What is the best way to calculate the index? I just came up with to use Int((n+1)/2)
, round(Int, (n+1)/2))
and Int((n+1)//2)
. Which is better or don't I need to too worry about them?
julia
You may want to see the update to my answer.
– hckr
Nov 25 '18 at 18:29
add a comment |
I have a odd number n
and want to use (n+1)/2
as an array index. What is the best way to calculate the index? I just came up with to use Int((n+1)/2)
, round(Int, (n+1)/2))
and Int((n+1)//2)
. Which is better or don't I need to too worry about them?
julia
I have a odd number n
and want to use (n+1)/2
as an array index. What is the best way to calculate the index? I just came up with to use Int((n+1)/2)
, round(Int, (n+1)/2))
and Int((n+1)//2)
. Which is better or don't I need to too worry about them?
julia
julia
asked Nov 25 '18 at 7:30
PaalonPaalon
184
184
You may want to see the update to my answer.
– hckr
Nov 25 '18 at 18:29
add a comment |
You may want to see the update to my answer.
– hckr
Nov 25 '18 at 18:29
You may want to see the update to my answer.
– hckr
Nov 25 '18 at 18:29
You may want to see the update to my answer.
– hckr
Nov 25 '18 at 18:29
add a comment |
1 Answer
1
active
oldest
votes
For better performance, you need integer division (div
or ÷
) for that. /
gives floating point results for integer arguments. //
gives a Rational
not an integer. So you need to write div(n+1, 2)
or (n+1) ÷ 2
. To type ÷
you can write div
and then press TAB on julia REPL, Jupyter notebook, Atom, etc.
Even if the dividend (n+1) is even, you need integer division to obtain an integer result directly, otherwise you need to convert the result to integer which will in turn be costly compared to the integer division.
You may also use right bit shift operator >>
or unsigned right bit shift operator >>>
, as positive integer division by 2^n
corresponds to shifting bits of that integer to the right n times. Although integer division by a power of 2 will be lowered to bit shift operation(s) by the compiler, the compiled code will still have an extra step if the dividend is a signed integer (i.e. Int and not UInt). Therefore, using the right bit shift operators instead may give better performance, although this is likely to be a premature optimization and affects the readability of your code.
The results of >>
and >>>
with negative integers will be different than that of the integer division (div
).
Also note that using unsigned right bit shift operator >>>
might save you from some integer overflow issues.
div(x, y)
÷(x, y)
The quotient from Euclidean division. Computes x/y, truncated to an
integer.
julia> 3/2 # returns a floating point number
1.5
julia> julia> 4/2
2.0
julia> 3//2 # returns a Rational
3//2
# now integer divison
julia> div(3, 2) # returns an integer
1
julia> 3 ÷ 2 # this is the same as div(3, 2)
1
julia> 9 >> 1 # this divides a positive integer by 2
4
julia> 9 >>> 1 # this also divides a positive integer by 2
4
# results with negative numbers
julia> -5 ÷ 2
-2
julia> -5 >> 1
-3
julia> -5 >>> 1
9223372036854775805
# results with overflowing (wrapping-around) argument
julia> (Int8(127) + Int8(3)) ÷ 2 # 127 is the largest Int8 integer
-63
julia> (Int8(127) + Int8(3)) >> 1
-63
julia> (Int8(127) + Int8(3)) >>> 1 # still gives 65 (130 ÷ 2)
65
You can use @code_native
macro to see how things are compiled to native code. Please do not forget more instructions does not necessarily imply being slower, although here it is be the case.
julia> f(a) = a ÷ 2
f (generic function with 2 methods)
julia> g(a) = a >> 1
g (generic function with 2 methods)
julia> h(a) = a >>> 1
h (generic function with 1 method)
julia> @code_native f(5)
.text
; Function f {
; Location: REPL[61]:1
; Function div; {
; Location: REPL[61]:1
movq %rdi, %rax
shrq $63, %rax
leaq (%rax,%rdi), %rax
sarq %rax
;}
retq
nop
;}
julia> @code_native g(5)
.text
; Function g {
; Location: REPL[62]:1
; Function >>; {
; Location: int.jl:448
; Function >>; {
; Location: REPL[62]:1
sarq %rdi
;}}
movq %rdi, %rax
retq
nopw (%rax,%rax)
;}
julia> @code_native h(5)
.text
; Function h {
; Location: REPL[63]:1
; Function >>>; {
; Location: int.jl:452
; Function >>>; {
; Location: REPL[63]:1
shrq %rdi
;}}
movq %rdi, %rax
retq
nopw (%rax,%rax)
;}
1
How about(n+1)>>1
?
– rickhg12hs
Nov 25 '18 at 15:49
1
@rickhg12hs added bit shift operators, the answer has become very long and complicated though. Also note that not every array has positive indices, so the effect of using bit shift operators to index arrays with negative indices (e.g.OffsetArrays.jl
) will be different than the Euclidean division.
– hckr
Nov 25 '18 at 18:28
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53465513%2fwhich-is-better-to-use-intn1-2-roundint-n1-2-or-intn1-2%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
For better performance, you need integer division (div
or ÷
) for that. /
gives floating point results for integer arguments. //
gives a Rational
not an integer. So you need to write div(n+1, 2)
or (n+1) ÷ 2
. To type ÷
you can write div
and then press TAB on julia REPL, Jupyter notebook, Atom, etc.
Even if the dividend (n+1) is even, you need integer division to obtain an integer result directly, otherwise you need to convert the result to integer which will in turn be costly compared to the integer division.
You may also use right bit shift operator >>
or unsigned right bit shift operator >>>
, as positive integer division by 2^n
corresponds to shifting bits of that integer to the right n times. Although integer division by a power of 2 will be lowered to bit shift operation(s) by the compiler, the compiled code will still have an extra step if the dividend is a signed integer (i.e. Int and not UInt). Therefore, using the right bit shift operators instead may give better performance, although this is likely to be a premature optimization and affects the readability of your code.
The results of >>
and >>>
with negative integers will be different than that of the integer division (div
).
Also note that using unsigned right bit shift operator >>>
might save you from some integer overflow issues.
div(x, y)
÷(x, y)
The quotient from Euclidean division. Computes x/y, truncated to an
integer.
julia> 3/2 # returns a floating point number
1.5
julia> julia> 4/2
2.0
julia> 3//2 # returns a Rational
3//2
# now integer divison
julia> div(3, 2) # returns an integer
1
julia> 3 ÷ 2 # this is the same as div(3, 2)
1
julia> 9 >> 1 # this divides a positive integer by 2
4
julia> 9 >>> 1 # this also divides a positive integer by 2
4
# results with negative numbers
julia> -5 ÷ 2
-2
julia> -5 >> 1
-3
julia> -5 >>> 1
9223372036854775805
# results with overflowing (wrapping-around) argument
julia> (Int8(127) + Int8(3)) ÷ 2 # 127 is the largest Int8 integer
-63
julia> (Int8(127) + Int8(3)) >> 1
-63
julia> (Int8(127) + Int8(3)) >>> 1 # still gives 65 (130 ÷ 2)
65
You can use @code_native
macro to see how things are compiled to native code. Please do not forget more instructions does not necessarily imply being slower, although here it is be the case.
julia> f(a) = a ÷ 2
f (generic function with 2 methods)
julia> g(a) = a >> 1
g (generic function with 2 methods)
julia> h(a) = a >>> 1
h (generic function with 1 method)
julia> @code_native f(5)
.text
; Function f {
; Location: REPL[61]:1
; Function div; {
; Location: REPL[61]:1
movq %rdi, %rax
shrq $63, %rax
leaq (%rax,%rdi), %rax
sarq %rax
;}
retq
nop
;}
julia> @code_native g(5)
.text
; Function g {
; Location: REPL[62]:1
; Function >>; {
; Location: int.jl:448
; Function >>; {
; Location: REPL[62]:1
sarq %rdi
;}}
movq %rdi, %rax
retq
nopw (%rax,%rax)
;}
julia> @code_native h(5)
.text
; Function h {
; Location: REPL[63]:1
; Function >>>; {
; Location: int.jl:452
; Function >>>; {
; Location: REPL[63]:1
shrq %rdi
;}}
movq %rdi, %rax
retq
nopw (%rax,%rax)
;}
1
How about(n+1)>>1
?
– rickhg12hs
Nov 25 '18 at 15:49
1
@rickhg12hs added bit shift operators, the answer has become very long and complicated though. Also note that not every array has positive indices, so the effect of using bit shift operators to index arrays with negative indices (e.g.OffsetArrays.jl
) will be different than the Euclidean division.
– hckr
Nov 25 '18 at 18:28
add a comment |
For better performance, you need integer division (div
or ÷
) for that. /
gives floating point results for integer arguments. //
gives a Rational
not an integer. So you need to write div(n+1, 2)
or (n+1) ÷ 2
. To type ÷
you can write div
and then press TAB on julia REPL, Jupyter notebook, Atom, etc.
Even if the dividend (n+1) is even, you need integer division to obtain an integer result directly, otherwise you need to convert the result to integer which will in turn be costly compared to the integer division.
You may also use right bit shift operator >>
or unsigned right bit shift operator >>>
, as positive integer division by 2^n
corresponds to shifting bits of that integer to the right n times. Although integer division by a power of 2 will be lowered to bit shift operation(s) by the compiler, the compiled code will still have an extra step if the dividend is a signed integer (i.e. Int and not UInt). Therefore, using the right bit shift operators instead may give better performance, although this is likely to be a premature optimization and affects the readability of your code.
The results of >>
and >>>
with negative integers will be different than that of the integer division (div
).
Also note that using unsigned right bit shift operator >>>
might save you from some integer overflow issues.
div(x, y)
÷(x, y)
The quotient from Euclidean division. Computes x/y, truncated to an
integer.
julia> 3/2 # returns a floating point number
1.5
julia> julia> 4/2
2.0
julia> 3//2 # returns a Rational
3//2
# now integer divison
julia> div(3, 2) # returns an integer
1
julia> 3 ÷ 2 # this is the same as div(3, 2)
1
julia> 9 >> 1 # this divides a positive integer by 2
4
julia> 9 >>> 1 # this also divides a positive integer by 2
4
# results with negative numbers
julia> -5 ÷ 2
-2
julia> -5 >> 1
-3
julia> -5 >>> 1
9223372036854775805
# results with overflowing (wrapping-around) argument
julia> (Int8(127) + Int8(3)) ÷ 2 # 127 is the largest Int8 integer
-63
julia> (Int8(127) + Int8(3)) >> 1
-63
julia> (Int8(127) + Int8(3)) >>> 1 # still gives 65 (130 ÷ 2)
65
You can use @code_native
macro to see how things are compiled to native code. Please do not forget more instructions does not necessarily imply being slower, although here it is be the case.
julia> f(a) = a ÷ 2
f (generic function with 2 methods)
julia> g(a) = a >> 1
g (generic function with 2 methods)
julia> h(a) = a >>> 1
h (generic function with 1 method)
julia> @code_native f(5)
.text
; Function f {
; Location: REPL[61]:1
; Function div; {
; Location: REPL[61]:1
movq %rdi, %rax
shrq $63, %rax
leaq (%rax,%rdi), %rax
sarq %rax
;}
retq
nop
;}
julia> @code_native g(5)
.text
; Function g {
; Location: REPL[62]:1
; Function >>; {
; Location: int.jl:448
; Function >>; {
; Location: REPL[62]:1
sarq %rdi
;}}
movq %rdi, %rax
retq
nopw (%rax,%rax)
;}
julia> @code_native h(5)
.text
; Function h {
; Location: REPL[63]:1
; Function >>>; {
; Location: int.jl:452
; Function >>>; {
; Location: REPL[63]:1
shrq %rdi
;}}
movq %rdi, %rax
retq
nopw (%rax,%rax)
;}
1
How about(n+1)>>1
?
– rickhg12hs
Nov 25 '18 at 15:49
1
@rickhg12hs added bit shift operators, the answer has become very long and complicated though. Also note that not every array has positive indices, so the effect of using bit shift operators to index arrays with negative indices (e.g.OffsetArrays.jl
) will be different than the Euclidean division.
– hckr
Nov 25 '18 at 18:28
add a comment |
For better performance, you need integer division (div
or ÷
) for that. /
gives floating point results for integer arguments. //
gives a Rational
not an integer. So you need to write div(n+1, 2)
or (n+1) ÷ 2
. To type ÷
you can write div
and then press TAB on julia REPL, Jupyter notebook, Atom, etc.
Even if the dividend (n+1) is even, you need integer division to obtain an integer result directly, otherwise you need to convert the result to integer which will in turn be costly compared to the integer division.
You may also use right bit shift operator >>
or unsigned right bit shift operator >>>
, as positive integer division by 2^n
corresponds to shifting bits of that integer to the right n times. Although integer division by a power of 2 will be lowered to bit shift operation(s) by the compiler, the compiled code will still have an extra step if the dividend is a signed integer (i.e. Int and not UInt). Therefore, using the right bit shift operators instead may give better performance, although this is likely to be a premature optimization and affects the readability of your code.
The results of >>
and >>>
with negative integers will be different than that of the integer division (div
).
Also note that using unsigned right bit shift operator >>>
might save you from some integer overflow issues.
div(x, y)
÷(x, y)
The quotient from Euclidean division. Computes x/y, truncated to an
integer.
julia> 3/2 # returns a floating point number
1.5
julia> julia> 4/2
2.0
julia> 3//2 # returns a Rational
3//2
# now integer divison
julia> div(3, 2) # returns an integer
1
julia> 3 ÷ 2 # this is the same as div(3, 2)
1
julia> 9 >> 1 # this divides a positive integer by 2
4
julia> 9 >>> 1 # this also divides a positive integer by 2
4
# results with negative numbers
julia> -5 ÷ 2
-2
julia> -5 >> 1
-3
julia> -5 >>> 1
9223372036854775805
# results with overflowing (wrapping-around) argument
julia> (Int8(127) + Int8(3)) ÷ 2 # 127 is the largest Int8 integer
-63
julia> (Int8(127) + Int8(3)) >> 1
-63
julia> (Int8(127) + Int8(3)) >>> 1 # still gives 65 (130 ÷ 2)
65
You can use @code_native
macro to see how things are compiled to native code. Please do not forget more instructions does not necessarily imply being slower, although here it is be the case.
julia> f(a) = a ÷ 2
f (generic function with 2 methods)
julia> g(a) = a >> 1
g (generic function with 2 methods)
julia> h(a) = a >>> 1
h (generic function with 1 method)
julia> @code_native f(5)
.text
; Function f {
; Location: REPL[61]:1
; Function div; {
; Location: REPL[61]:1
movq %rdi, %rax
shrq $63, %rax
leaq (%rax,%rdi), %rax
sarq %rax
;}
retq
nop
;}
julia> @code_native g(5)
.text
; Function g {
; Location: REPL[62]:1
; Function >>; {
; Location: int.jl:448
; Function >>; {
; Location: REPL[62]:1
sarq %rdi
;}}
movq %rdi, %rax
retq
nopw (%rax,%rax)
;}
julia> @code_native h(5)
.text
; Function h {
; Location: REPL[63]:1
; Function >>>; {
; Location: int.jl:452
; Function >>>; {
; Location: REPL[63]:1
shrq %rdi
;}}
movq %rdi, %rax
retq
nopw (%rax,%rax)
;}
For better performance, you need integer division (div
or ÷
) for that. /
gives floating point results for integer arguments. //
gives a Rational
not an integer. So you need to write div(n+1, 2)
or (n+1) ÷ 2
. To type ÷
you can write div
and then press TAB on julia REPL, Jupyter notebook, Atom, etc.
Even if the dividend (n+1) is even, you need integer division to obtain an integer result directly, otherwise you need to convert the result to integer which will in turn be costly compared to the integer division.
You may also use right bit shift operator >>
or unsigned right bit shift operator >>>
, as positive integer division by 2^n
corresponds to shifting bits of that integer to the right n times. Although integer division by a power of 2 will be lowered to bit shift operation(s) by the compiler, the compiled code will still have an extra step if the dividend is a signed integer (i.e. Int and not UInt). Therefore, using the right bit shift operators instead may give better performance, although this is likely to be a premature optimization and affects the readability of your code.
The results of >>
and >>>
with negative integers will be different than that of the integer division (div
).
Also note that using unsigned right bit shift operator >>>
might save you from some integer overflow issues.
div(x, y)
÷(x, y)
The quotient from Euclidean division. Computes x/y, truncated to an
integer.
julia> 3/2 # returns a floating point number
1.5
julia> julia> 4/2
2.0
julia> 3//2 # returns a Rational
3//2
# now integer divison
julia> div(3, 2) # returns an integer
1
julia> 3 ÷ 2 # this is the same as div(3, 2)
1
julia> 9 >> 1 # this divides a positive integer by 2
4
julia> 9 >>> 1 # this also divides a positive integer by 2
4
# results with negative numbers
julia> -5 ÷ 2
-2
julia> -5 >> 1
-3
julia> -5 >>> 1
9223372036854775805
# results with overflowing (wrapping-around) argument
julia> (Int8(127) + Int8(3)) ÷ 2 # 127 is the largest Int8 integer
-63
julia> (Int8(127) + Int8(3)) >> 1
-63
julia> (Int8(127) + Int8(3)) >>> 1 # still gives 65 (130 ÷ 2)
65
You can use @code_native
macro to see how things are compiled to native code. Please do not forget more instructions does not necessarily imply being slower, although here it is be the case.
julia> f(a) = a ÷ 2
f (generic function with 2 methods)
julia> g(a) = a >> 1
g (generic function with 2 methods)
julia> h(a) = a >>> 1
h (generic function with 1 method)
julia> @code_native f(5)
.text
; Function f {
; Location: REPL[61]:1
; Function div; {
; Location: REPL[61]:1
movq %rdi, %rax
shrq $63, %rax
leaq (%rax,%rdi), %rax
sarq %rax
;}
retq
nop
;}
julia> @code_native g(5)
.text
; Function g {
; Location: REPL[62]:1
; Function >>; {
; Location: int.jl:448
; Function >>; {
; Location: REPL[62]:1
sarq %rdi
;}}
movq %rdi, %rax
retq
nopw (%rax,%rax)
;}
julia> @code_native h(5)
.text
; Function h {
; Location: REPL[63]:1
; Function >>>; {
; Location: int.jl:452
; Function >>>; {
; Location: REPL[63]:1
shrq %rdi
;}}
movq %rdi, %rax
retq
nopw (%rax,%rax)
;}
edited Nov 26 '18 at 22:01
answered Nov 25 '18 at 7:56
hckrhckr
1,843920
1,843920
1
How about(n+1)>>1
?
– rickhg12hs
Nov 25 '18 at 15:49
1
@rickhg12hs added bit shift operators, the answer has become very long and complicated though. Also note that not every array has positive indices, so the effect of using bit shift operators to index arrays with negative indices (e.g.OffsetArrays.jl
) will be different than the Euclidean division.
– hckr
Nov 25 '18 at 18:28
add a comment |
1
How about(n+1)>>1
?
– rickhg12hs
Nov 25 '18 at 15:49
1
@rickhg12hs added bit shift operators, the answer has become very long and complicated though. Also note that not every array has positive indices, so the effect of using bit shift operators to index arrays with negative indices (e.g.OffsetArrays.jl
) will be different than the Euclidean division.
– hckr
Nov 25 '18 at 18:28
1
1
How about
(n+1)>>1
?– rickhg12hs
Nov 25 '18 at 15:49
How about
(n+1)>>1
?– rickhg12hs
Nov 25 '18 at 15:49
1
1
@rickhg12hs added bit shift operators, the answer has become very long and complicated though. Also note that not every array has positive indices, so the effect of using bit shift operators to index arrays with negative indices (e.g.
OffsetArrays.jl
) will be different than the Euclidean division.– hckr
Nov 25 '18 at 18:28
@rickhg12hs added bit shift operators, the answer has become very long and complicated though. Also note that not every array has positive indices, so the effect of using bit shift operators to index arrays with negative indices (e.g.
OffsetArrays.jl
) will be different than the Euclidean division.– hckr
Nov 25 '18 at 18:28
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53465513%2fwhich-is-better-to-use-intn1-2-roundint-n1-2-or-intn1-2%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You may want to see the update to my answer.
– hckr
Nov 25 '18 at 18:29