I don’t know how to ask, but just want to ask. Help me tag it, please. Anyway, my friend asked me a question which one is faster in Java
int a = 5 + 5 + 5 + 5 + 5
or
int b = 5 * 5 ?
Is he language dependent? I mean, afaster than bin java but not in C
a
b
my answer is afaster than bcomparing addition / multiplication in a computer organization
It depends on the platform (and the compiler). If you need to know, then measure it. It is unlikely that you will find yourself in a situation where you need to know.
( ); ., , http://en.wikipedia.org/wiki/Constant_folding.
. :
public class Toto { public static void main(String[] args) { int a = 5 + 5 + 5 + 5 + 5; int b = 5 * 5; } }
:
public class Toto { public static void main(String args[]) { byte byte0 = 25; byte byte1 = 25; } }
.
,
int a = 25; int b = 25;
( 100% , , ).
, , JIT, 1:1 , , , ( , , : 1 3 , - ).
, , , ( 1 , , 4 ).
, , , (5<<2+5).
5<<2+5
: , . ( , Java-to-bytecode, JIT).
-, "" . , , . , , . , , , X86 . "" , . , , , , . , , " ", .
, Hotspot 32- Pentium ( Intel, , , , , ). , : , ; .
, , , , 2 , JIT- . , , x 1/x, x - 2.
, , Java, JIT- , , . , , , "5 + 5 + 5 + 5 + 5", Java "25".
. f1(n) = n * c, f2(n) = Sum[1->n] c.
f1(n) = n * c
f2(n) = Sum[1->n] c
- O(1) ( , n), O(n) ( , n).
O(1)
n
O(n)
, :
? , .
? , NOP ( ).
? , , , () , add-operation
, , , , .
, , , , : . A B, , // , , , .
, , , .
, .
I assume that Addition is faster than multiplication, because (as far as I know) all multiplications are considered as additions
Added:
read here for some explanation
Source: https://habr.com/ru/post/1786892/More articles:Change page controls from an Xslt helper class in Sitecore - xsltMath Kit Library / Header - c ++Choosing an NSOpenPanel Constraint for the iPhoto Library - cocoaДвигатель уведомления о событиях - MSMQ - c#creating an HTML source introduces a default value in tinyMCE - tinymcePoor performance (stuttering) with Windows Phone 7 Page Transitions - performancehow to install ndk (linux)? - androidSustained request per second [QPS] without timeout errors - google-app-engineSpring and Hibernate operations - springdelete double lines in a line - stringAll Articles