Compile-time calculation: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (→C (simpler version): Remove bogus template markup) |
|||
Line 1,003: | Line 1,003: | ||
If we compile this with <code>rustc factorial.rs -O --emit asm</code> and inspect the outputted assembly, we can see <code>movq $3628800, (%rsp)</code>. This means the result of 3628800 was calculated in compile-time rather than run-time. |
If we compile this with <code>rustc factorial.rs -O --emit asm</code> and inspect the outputted assembly, we can see <code>movq $3628800, (%rsp)</code>. This means the result of 3628800 was calculated in compile-time rather than run-time. |
||
=={{header|Scala}}== |
|||
<lang Scala>object Main extends { |
|||
val tenFactorial = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 |
|||
def tenFac = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 |
|||
println(s"10! = $tenFactorial", tenFac) |
|||
}</lang> |
|||
As it can been seen in the always heavily optimized run-time code the calculations are already computed for the field constant and function method. |
|||
<pre> |
|||
public int tenFac(); |
|||
descriptor: ()I |
|||
flags: (0x0001) ACC_PUBLIC |
|||
Code: |
|||
stack=1, locals=1, args_size=1 |
|||
0: ldc #28 // int 3628800 |
|||
2: ireturn |
|||
LocalVariableTable: |
|||
Start Length Slot Name Signature |
|||
0 3 0 this L$line2/$read$$iw$$iw$Main$; |
|||
LineNumberTable: |
|||
line 14: 0 |
|||
public $line2.$read$$iw$$iw$Main$(); |
|||
descriptor: ()V |
|||
flags: (0x0001) ACC_PUBLIC |
|||
Code: |
|||
stack=6, locals=1, args_size=1 |
|||
0: aload_0 |
|||
1: invokespecial #29 // Method java/lang/Object."<init>":()V |
|||
4: aload_0 |
|||
5: putstatic #31 // Field MODULE$:L$line2/$read$$iw$$iw$Main$; |
|||
8: aload_0 |
|||
9: ldc #28 // int 3628800 |
|||
11: putfield #25 // Field tenFactorial:I |
|||
14: getstatic #36 // Field scala/Predef$.MODULE$:Lscala/Predef$; |
|||
17: new #38 // class scala/Tuple2 |
|||
20: dup |
|||
21: new #40 // class java/lang/StringBuilder</pre> |
|||
=={{header|Seed7}}== |
=={{header|Seed7}}== |
||