Skip to content

Commit

Permalink
Sort rational numbers and primes first benchmarks (#10142)
Browse files Browse the repository at this point in the history
Investigating #6361 - can we speed callback from `java.util.TimSort.sort` to Enso by using `CallTarget`?
  • Loading branch information
JaroslavTulach authored Jun 9, 2024
1 parent 54b806a commit b5969cf
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 11 deletions.
4 changes: 2 additions & 2 deletions test/Benchmarks/src/Vector/Distinct.enso
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ collect_benches = Bench.build builder->
data = Data.create

builder.group "Vector_Distinct" options group_builder->
group_builder.specify "Random_Integer_Vector_Distinct" <|
group_builder.specify "Random_Integer_Vector_Distinct_v2" <|
data.random_vec.distinct
group_builder.specify "Uniform_Integer_Vector_Distinct" <|
data.uniform_vec.distinct
group_builder.specify "Random_Text_Vector_Distinct" <|
group_builder.specify "Random_Text_Vector_Distinct_v2" <|
data.random_text_vec.distinct
group_builder.specify "Uniform_Text_Vector_Distinct" <|
data.uniform_text_vec.distinct
Expand Down
55 changes: 47 additions & 8 deletions test/Benchmarks/src/Vector/Sort.enso
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,30 @@ type Int
identity self = self


type Int_Comparator
compare a:Int b:Int = Ordering.compare a.v b.v
hash i:Int = 3721 + Default_Comparator.hash_builtin i.v

Comparable.from(_:Int) = Int_Comparator


# The Benchmarks ==============================================================


options = Bench.options . set_warmup (Bench.phase_conf 1 4) . set_measure (Bench.phase_conf 1 3)
options = Bench.options . set_warmup (Bench.phase_conf 3 4) . set_measure (Bench.phase_conf 3 1)


type Data
Value ~sorted_vec ~partially_sorted_vec ~random_vec ~random_vec_wrapped
Value ~sorted_vec ~partially_sorted_vec ~random_vec ~random_vec_wrapped ~rational_vec ~integer_vec

create vec_size =
Data.Value (Data.make_sorted_vec vec_size) (make_partially_sorted_vec vec_size) (Data.make_random_vec vec_size) ((Data.make_random_vec vec_size).map (v -> Int.Value v))
f1 s = Data.make_sorted_vec s
f2 s = make_partially_sorted_vec s
f3 s = Data.make_random_vec s
f4 s = (Data.make_random_vec s).map (v -> Int.Value v)
f5 s = Utils.make_random_rational_vec s
f6 s = f5 s . map (x-> x.to_float . floor + 2)
Data.Value (f1 vec_size) (f2 vec_size) (f3 vec_size) (f4 vec_size) (f5 vec_size) (f6 vec_size)

make_sorted_vec vec_size =
make_sorted_ascending_vec vec_size
Expand Down Expand Up @@ -89,13 +102,13 @@ collect_benches = Bench.build builder->
group_builder.specify "Sorted_Runs_Descending" <|
data.partially_sorted_vec.sort Sort_Direction.Descending

group_builder.specify "Random_Elements_Ascending" <|
group_builder.specify "Random_Elements_Ascending_v2" <|
data.random_vec.sort

group_builder.specify "Random_Elements_Descending" <|
group_builder.specify "Random_Elements_Descending_v2" <|
data.random_vec.sort Sort_Direction.Descending

group_builder.specify "Sorting_with_a_Custom_Projection" <|
group_builder.specify "Sorting_with_a_Custom_Projection_v2" <|
data.random_vec.sort on=projection

group_builder.specify "Sorting_with_an_identity_function" <|
Expand All @@ -104,8 +117,34 @@ collect_benches = Bench.build builder->
group_builder.specify "Sorting_with_an_unresolved_identity_function" <|
data.random_vec_wrapped.sort on=(.identity)

group_builder.specify "Sorting_with_the_Default_Ordered_Comparator" <|
group_builder.specify "Sorting_with_the_Default_Ordered_Comparator_v2" <|
data.random_vec.sort by=comparator

group_builder.specify "Sorting_Prime_Numbers_First" <|
is_prime p -> Boolean =
test n = if n*n > p then True else
if p % n == 0 then False else
@Tail_Call test n+1

test 2

prime_first a:Integer b:Integer -> Ordering =
prime_a = is_prime a
prime_b = is_prime b

if prime_a == prime_b then Ordering.compare a b else
if prime_a then Ordering.Less else
Ordering.Greater

data.integer_vec.sort by=prime_first

group_builder.specify "Sort_Rational_Vector" <|
data.rational_vec.sort

group_builder.specify "Sort_Rational_Vector_by_comparator" <|
data.rational_vec.sort by=(a-> b-> b.compare a)

group_builder.specify "Sort_Rational_Vector_by_converting_to_float" <|
data.rational_vec.sort on=(.to_float)

main = collect_benches . run_main
main filter=Nothing = collect_benches . run_main filter
45 changes: 44 additions & 1 deletion test/Benchmarks/src/Vector/Utils.enso
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,47 @@ polyglot java import java.util.Random as Java_Random
make_random_vec : Integer -> Vector
make_random_vec n =
random_gen = Java_Random.new n
Vector.fill n random_gen.nextDouble
Vector.new n (_-> random_gen.nextDouble)

make_random_rational_vec n =
random_gen = Java_Random.new n
Vector.new n _->
p = random_gen.nextInt 100
q = 1 + random_gen.nextInt 99
Rational.new p q

type Rational
private Fraction p:Integer q:Integer

new p:Integer q:Integer=1 =
if q == 0 then Error.throw "Denominator cannot be zero" else
Rational.Fraction p q

compare self that:Rational -> Ordering = Ordering.compare self.p*that.q that.p*self.q

normalize self = if self.p == 0 then Rational.new 0 1 else
gcd a b = case Ordering.compare a b of
Ordering.Greater -> @Tail_Call gcd (a.rem b) b
Ordering.Less -> @Tail_Call gcd a (b.rem a)
Ordering.Equal -> a

d = gcd self.p self.q

Rational.new self.p/d self.q/d

to_float self = self.p / self.q

to_text self = self.p.to_text+"/"+self.q.to_text

Float.from (that : Rational) = that.to_float

type Rational_Comparator
compare (a:Rational) (b:Rational) = Ordering.compare a.p*b.q b.p*a.q
hash (v:Rational) =
n = v.normalize
n.p*37 + n.q*51


Comparable.from (_ : Rational) = Rational_Comparator

main = Standard.Base.Runtime.Debug.breakpoint

0 comments on commit b5969cf

Please sign in to comment.