array-mult.zy 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // this is a straight port of the Scheme code in array-mult.scm
  2. (defn multArrayLoop [a b res i]
  3. (cond (== i (len a)) res
  4. (begin
  5. (aset res i (* (aget a i) (aget b i)))
  6. (multArrayLoop a b res (+ i 1)))))
  7. (defn multArray [a b]
  8. (multArrayLoop a b (makeArray (len a)) 0))
  9. (defn randomArray [arr i]
  10. (cond (== i (len arr))
  11. arr
  12. (begin
  13. (aset arr i (random))
  14. (randomArray arr (+ i 1)))))
  15. (defn doInLoop [fx times]
  16. (cond (== times 0) nil
  17. (begin
  18. (fx)
  19. (doInLoop fx (- times 1)))))
  20. //
  21. (let [
  22. a (randomArray (makeArray 100) 0)
  23. b (randomArray (makeArray 100) 0)
  24. ]
  25. (doInLoop (fn [] (multArray a b)) 200))
  26. /* on my laptop, about 34x slower than Chez scheme.
  27. jaten@jatens-MacBook-Pro ~/go/src/github.com/glycerine/zygomys/benchmarks (master) $ time zygo ./array-mult.zy
  28. real 0m4.322s
  29. user 0m5.737s
  30. sys 0m0.191s
  31. jaten@jatens-MacBook-Pro ~/go/src/github.com/glycerine/zygomys/benchmarks (master) $ time scheme --script ./array-mult.scm
  32. real 0m0.169s
  33. user 0m0.136s
  34. sys 0m0.021s
  35. $ gi -q
  36. gi> = 5.737 / 0.169
  37. 33.94674556213
  38. */