Advertisement
Guest User

merge_benchmark.jl

a guest
Jan 6th, 2019
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Julia 2.37 KB | None | 0 0
  1. struct B{T}
  2.     val::T
  3. end
  4.  
  5. f(x::B{T}, y::B{T}) where T = B{T}(x.val + y.val) #always returns same type as x and y
  6.  
  7.  
  8. __merge(f, unused_ys, x_i) = x_i, unused_ys
  9. __merge(f, unused_ys, x_i::T, y_j::T, y...) where T = f(x_i, y_j), (unused_ys..., y...)
  10. __merge(f, unused_ys, x_i, y_j, y...) = __merge(f, (unused_ys..., y_j), x_i, y...)
  11.  
  12. _merge(f, results, remaining_ys) = (results..., remaining_ys...)
  13. function _merge(f, results, remaining_ys, x_i, x...)
  14.     _result, _remaining_ys = __merge(f, (), x_i, remaining_ys...)
  15.     return _merge(f, (results..., _result), _remaining_ys, x...)
  16. end
  17.  
  18. merge(f, x, y) = _merge(f, (), y, x...)
  19.  
  20. @inline _mergeo(f, results, remaining_ys) = (results, remaining_ys)
  21. @inline function _mergeo(f, results, remaining_ys, x_i, x...)
  22.     _result, _remaining_ys = __merge(f, (), x_i, remaining_ys...)
  23.     return _mergeo(f, (results..., _result), _remaining_ys, x...)
  24. end
  25.  
  26. @inline function mergeo(f, x::T, y) where T
  27.     let (res::T, rem) = _mergeo(f, (), y, x...)
  28.         return (res..., rem...)
  29.     end
  30. end
  31.  
  32.  
  33. _merge1(f, x, y_acc) = (x, y_acc...)
  34. _merge1(f, x::T, y_acc, y::T, y_rest...) where T = (f(x, y), y_acc..., y_rest...)
  35. _merge1(f, x, y_acc, y, y_rest...) = _merge1(f, x, (y, y_acc...), y_rest...)
  36. merge1(f, x, ys) = _merge1(f, x, (), ys...)
  37.  
  38. merge2(f, ::Tuple{}, ys) = ys
  39. merge2(f::F, xs, ys) where F = merge2(f, Base.tail(xs), merge1(f, first(xs), ys))
  40.  
  41. using BenchmarkTools, Random
  42.  
  43. const types = [ map.([identity,unsigned], Ref([Int8, Int16, Int32, Int64, Int128])) |>
  44.                     x -> [x...;];
  45.                 map.([identity,T->Complex{T}], Ref([Float16, Float32, Float64])) |>
  46.                     x -> [x...;] ]
  47.  
  48. function benchmark_merges(xargs, yargs, xysame=0)
  49.     shuffled_types = shuffle(types)
  50.     xtypes = ( (shuffled_types[1:xargs] |> shuffle)...,)
  51.     ytypes = ( (shuffled_types[range(xargs-xysame+1, length=yargs)] |> shuffle)...,)
  52.  
  53.     println("xtypes = $xtypes, ytypes = $ytypes")
  54.  
  55.     xs, ys = (rand.(xtypes), rand.(ytypes))
  56.  
  57.     println("\nmerge_tamas:\n" * repeat('-', 40))
  58.     show(stdout, MIME("text/plain"), @benchmark merge2(f, $xs, $ys))
  59.  
  60.     println("\n\nmerge_simeon:\n" * repeat('-', 40))
  61.     show(stdout, MIME("text/plain"), @benchmark merge(f, $xs, $ys))
  62.  
  63.     println("\n\nmerge_type-inferable_simeon:\n" * repeat('-', 40))
  64.     show(stdout, MIME("text/plain"), @benchmark mergeo(f, $xs, $ys))
  65. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement