Performance-of-numeric-operations.xhtml 5.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg">
  3. <head>
  4. <title>Performance of numeric operations</title>
  5. <link rel="stylesheet" type="text/css" href="docbook-epub.css"/>
  6. <link rel="stylesheet" type="text/css" href="kawa.css"/>
  7. <script src="kawa-ebook.js" type="text/javascript"/>
  8. <meta name="generator" content="DocBook XSL-NS Stylesheets V1.79.1"/>
  9. <link rel="prev" href="Logical-Number-Operations.xhtml" title="Logical Number Operations"/>
  10. <link rel="next" href="Characters-and-text.xhtml" title="Characters and text"/>
  11. </head>
  12. <body>
  13. <header/>
  14. <section class="sect1" title="Performance of numeric operations" epub:type="subchapter" id="Performance-of-numeric-operations">
  15. <div class="titlepage">
  16. <div>
  17. <div>
  18. <h2 class="title" style="clear: both">Performance of numeric operations</h2>
  19. </div>
  20. </div>
  21. </div>
  22. <p>Kawa can generally do a pretty good job of generating
  23. efficient code for numeric operations, at least when
  24. it knows or can figure out the types of the operands.
  25. </p>
  26. <p>The basic operations <code class="literal">+</code>, <code class="literal">-</code>, and <code class="literal">*</code>
  27. are compiled to single-instruction bytecode if both
  28. operands are <code class="literal">int</code> or <code class="literal">long</code>.
  29. Likewise, if both operands are floating-point (or
  30. one is floating-point and the other is rational),
  31. then single-instruction <code class="literal">double</code> or <code class="literal">float</code>
  32. instructions are emitted.
  33. </p>
  34. <p>A binary operation involving an infinite-precision <code class="literal">integer</code>
  35. and a fixed-size <code class="literal">int</code> or <code class="literal">long</code> is normally
  36. evaluated by expanding the latter to <code class="literal">integer</code>
  37. and using <code class="literal">integer</code> arithmetic. An exception is
  38. an integer literal whose
  39. value fits in an <code class="literal">int</code> or <code class="literal">long</code> - in that case
  40. the operation is done using <code class="literal">int</code> or <code class="literal">long</code>
  41. arithmetic.
  42. </p>
  43. <p>In general, integer literals have amorphous type.
  44. When used to infer the type of a variable, they have <code class="literal">integer</code> type:
  45. </p>
  46. <pre class="screen">(let ((v1 0))
  47. ... v1 has type integer ... )
  48. </pre>
  49. <p>However, a literal whose value fits in the <code class="literal">int</code> or <code class="literal">long</code> range
  50. is implicitly viewed <code class="literal">int</code> or <code class="literal">long</code> in certain contexts,
  51. primarily method overload resolution and binary arithmetic
  52. (as mentioned above).
  53. </p>
  54. <p>The comparison functions <code class="literal">&lt;</code>, <code class="literal">&lt;=</code>, <code class="literal">=</code>,
  55. <code class="literal">&gt;</code>, and <code class="literal">=&gt;</code> are also optimized to single instriction
  56. operations if the operands have appropriate type.
  57. However, the functions <code class="literal">zero?</code>, <code class="literal">positive?</code>, and <code class="literal">negative?</code>
  58. have not yet been optimized.
  59. Instead of <code class="literal">(positive? x)</code> write <code class="literal">(&gt; x 0)</code>.
  60. </p>
  61. <p>There are a number of integer division and modulo operations.
  62. If the operands are <code class="literal">int</code> or <code class="literal">long</code>, it is faster
  63. to use <code class="literal">quotient</code> and <code class="literal">remainder</code> rather
  64. than <code class="literal">div</code> and <code class="literal">mod</code> (or <code class="literal">modulo</code>).
  65. If you know the first operand is non-negative and the second is positive,
  66. then use <code class="literal">quotient</code> and <code class="literal">remainder</code>.
  67. (If an operand is an arbitrary-precision <code class="literal">integer</code>,
  68. then it dosn’t really matter.)
  69. </p>
  70. <p>The logical operations <code class="literal">bitwise-and</code>, <code class="literal">bitwise-ior</code>,
  71. <code class="literal">bitwise-xor</code>, <code class="literal">bitwise-not</code>, <code class="literal">bitwise-arithmetic-shift-left</code>,
  72. <code class="literal">bitwise-arithmetic-shift-right</code> are compiled
  73. to single bitcode instructions if the operands are <code class="literal">int</code> or <code class="literal">long</code>.
  74. Avoid <code class="literal">bitwise-arithmetic-shift</code> if the sign of the shift is known.
  75. If the operands are arbitrary-precision <code class="literal">integer</code>,
  76. a library call is needed, but run-time type dispatch is avoided.
  77. </p>
  78. </section>
  79. <footer>
  80. <div class="navfooter">
  81. <p>
  82. Up: <a accesskey="u" href="Numbers.xhtml">Quantities and Numbers</a></p>
  83. <p>
  84. Previous: <a accesskey="p" href="Logical-Number-Operations.xhtml">Logical Number Operations</a></p>
  85. </div>
  86. </footer>
  87. </body>
  88. </html>