Guest User

Integer arithmetic slower than floating point arithmetic

a guest
Jun 10th, 2010
442
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 1.36 KB | None | 0 0
  1. /**
  2.  *
  3.  */
  4. package media
  5.  
  6. import org.junit.BeforeClass
  7. import org.junit.Test
  8. import org.apache.commons.lang.time.StopWatch
  9. import static org.junit.Assert.*
  10.  
  11. /**
  12.  * Compares the performance of integer and floating point arithmetic in Groovy.
  13.  *
  14.  * @author <a href="http://johannburkard.de">Johann Burkard</a>
  15.  * @see <a href="http://groovy-programming.com">Groovy Programming</a>
  16.  */
  17. class LongDivisionTest {
  18.    
  19.     static final long start = 1
  20.    
  21.     static final long tests = 100000
  22.    
  23.     @BeforeClass
  24.     static void setUpBeforeClass() {
  25.         println 'Warming up, this could take 5 s or so'
  26.         (start..tests).each {
  27.             tests / it
  28.             (double) tests / it
  29.         }
  30.     }
  31.    
  32.     @Test
  33.     void longDivision() {
  34.         StopWatch watch = new StopWatch()
  35.         watch.start()
  36.         (start..tests).each {
  37.             tests / it
  38.         }
  39.         println "long: ${watch.time}"
  40.     }
  41.  
  42.     @Test
  43.     void doubleDivision() {
  44.         StopWatch watch = new StopWatch()
  45.         watch.start()
  46.         (start..tests).each {
  47.             (double) tests / it
  48.         }
  49.         println "double: ${watch.time}"
  50.     }
  51.    
  52.     @Test
  53.     void deepLongDivision() {
  54.         deepLongDivision1()
  55.     }
  56.    
  57.     void deepLongDivision1() {
  58.         deepLongDivision2()
  59.     }
  60.    
  61.     void deepLongDivision2() {
  62.         deepLongDivision3()
  63.     }
  64.    
  65.     void deepLongDivision3() {
  66.         StopWatch watch = new StopWatch()
  67.         watch.start()
  68.         (start..tests).each {
  69.             tests / it
  70.         }
  71.         println "long (deep): ${watch.time}"
  72.     }
  73.    
  74. }
Advertisement
Add Comment
Please, Sign In to add comment