Skip to content

Instantly share code, notes, and snippets.

@jvelilla
Last active July 10, 2020 13:12
Show Gist options
  • Select an option

  • Save jvelilla/1bc0a9650d88f2766142e8adcfd6efb0 to your computer and use it in GitHub Desktop.

Select an option

Save jvelilla/1bc0a9650d88f2766142e8adcfd6efb0 to your computer and use it in GitHub Desktop.
note
description: "hash_reals application root class"
date: "$Date$"
revision: "$Revision$"
class
APPLICATION
inherit
ARGUMENTS_32
create
make
feature {NONE} -- Initialization
make
-- Run application.
local
timer: DT_STOPWATCH
l_table: REAL_32_HASH_TABLE [INTEGER]
l_table2: HASH_TABLE [INTEGER, REAL_32]
keys: ARRAY [REAL_32]
l_table_64: REAL_64_HASH_TABLE [INTEGER]
keys_64: ARRAY [REAL_64]
do
-- Insert: Keys Large Reals
print ("%NLarge Reals%N")
keys := generate_n_large_keys (Number_of_elements)
insert_on_hash_table (number_of_iterations, keys)
insert_on_real_32_hash_table (number_of_iterations, keys)
keys := generate_n_large_keys (Number_of_elements)
search_on_hash_table (number_of_iterations, keys)
search_on_real_32_hash_table (number_of_iterations, keys)
print ("%N%NReals using [0,1] * 1000 %N")
keys := generate_n_keys (10 * Number_of_elements, 1000)
insert_on_hash_table (number_of_iterations, keys)
insert_on_real_32_hash_table (number_of_iterations, keys)
keys := generate_n_keys (50 * Number_of_elements, 1000)
search_on_hash_table (number_of_iterations, keys)
search_on_real_32_hash_table (number_of_iterations, keys)
print ("%N%NReals using [0,1] * 10000 %N")
keys := generate_n_keys (10 * Number_of_elements, 10000)
insert_on_hash_table (number_of_iterations, keys)
insert_on_real_32_hash_table (number_of_iterations, keys)
keys := generate_n_keys (100 * Number_of_elements, 10000)
search_on_hash_table (number_of_iterations, keys)
search_on_real_32_hash_table (number_of_iterations, keys)
print ("%N%NReals using [0,1] * 100000 %N")
keys := generate_n_keys (10 * Number_of_elements, 100000)
insert_on_hash_table (number_of_iterations, keys)
insert_on_real_32_hash_table (number_of_iterations, keys)
keys := generate_n_keys (100 * Number_of_elements, 100000)
search_on_hash_table (number_of_iterations, keys)
search_on_real_32_hash_table (number_of_iterations, keys)
print ("%N%NReals using [0,1] * 500000 %N")
keys := generate_n_keys (10 * Number_of_elements, 500000)
insert_on_hash_table (number_of_iterations, keys)
insert_on_real_32_hash_table (number_of_iterations, keys)
keys := generate_n_keys (100 * Number_of_elements, 500000)
search_on_hash_table (number_of_iterations, keys)
search_on_real_32_hash_table (number_of_iterations, keys)
print ("%N%NReals using [0,1] * 1000000 %N")
keys := generate_n_keys (10 * Number_of_elements, 1000000)
insert_on_hash_table (number_of_iterations, keys)
insert_on_real_32_hash_table (number_of_iterations, keys)
keys := generate_n_keys (100 * Number_of_elements, 1000000)
search_on_hash_table (number_of_iterations, keys)
search_on_real_32_hash_table (number_of_iterations, keys)
-- Insert: Keys: small Reals 0..1
print ("%N%NSmall Reals 0..1 %N")
keys := generate_n_keys_range_0_1 (number_of_elements)
insert_on_hash_table (number_of_iterations, keys)
insert_on_real_32_hash_table (number_of_iterations, keys)
keys := generate_n_keys_range_0_1 (number_of_elements)
search_on_hash_table (number_of_iterations, keys)
search_on_real_32_hash_table (number_of_iterations, keys)
-- Insert: KeysNegative Reals
print ("%N%NNegative Reals%N")
keys := generate_n_negative_keys (10 * number_of_elements)
insert_on_hash_table (number_of_iterations, keys)
insert_on_real_32_hash_table (number_of_iterations, keys)
keys := generate_n_negative_keys (100 * number_of_elements)
search_on_hash_table (number_of_iterations, keys)
search_on_real_32_hash_table (number_of_iterations, keys)
-- Insert: Keys NAN Reals
print ("%N%NNAN Reals%N")
keys := generate_n_nan_keys (10 * number_of_elements)
insert_on_hash_table (number_of_iterations, keys)
insert_on_real_32_hash_table (number_of_iterations, keys)
keys := generate_n_nan_keys (100 * number_of_elements)
search_on_hash_table (number_of_iterations, keys)
search_on_real_32_hash_table (number_of_iterations, keys)
-- Insert: Keys Negativity Infinity Reals
print ("%N%NNegative_infinity Reals%N")
keys := generate_n_negative_infinity_keys (10 * number_of_elements)
insert_on_hash_table (number_of_iterations, keys)
insert_on_real_32_hash_table (number_of_iterations, keys)
keys := generate_n_negative_infinity_keys (100 * number_of_elements)
search_on_hash_table (number_of_iterations, keys)
search_on_real_32_hash_table (number_of_iterations, keys)
-- Insert: Keys Positive Infinity Reals
print ("%N%NPositive_infinity Reals%N")
keys := generate_n_positive_infinity_keys (10 * number_of_elements)
insert_on_hash_table (number_of_iterations, keys)
insert_on_real_32_hash_table (number_of_iterations, keys)
keys := generate_n_positive_infinity_keys (100 * number_of_elements)
search_on_hash_table (number_of_iterations, keys)
search_on_real_32_hash_table (number_of_iterations, keys)
-- -- Insert: Keys Mixed
-- print ("%N%NMixed Reals%N")
-- keys := generate_n_mixed_keys (number_of_elements)
-- insert_on_hash_table (number_of_iterations, keys)
-- insert_on_real_32_hash_table (number_of_iterations, keys)
-- search_on_hash_table (number_of_iterations, keys)
-- search_on_real_32_hash_table (number_of_iterations, keys)
-- REAL 64
----------------------------------------------------------------
----------------------------------------------------------------
-- Insert: Keys Large Reals
print ("%N%NLarge Reals 64%N")
keys_64 := generate_n_large_keys_64 (Number_of_elements)
insert_on_hash_table_64 (number_of_iterations, keys_64)
insert_on_real_64_hash_table (number_of_iterations, keys_64)
search_on_hash_table_64 (number_of_iterations, keys_64)
search_on_real_64_hash_table (number_of_iterations, keys_64)
print ("%NReals 64 using [0, 1] * 1000 %N")
keys_64 := generate_n_keys_64 (10 * Number_of_elements, 1000)
insert_on_hash_table_64 (number_of_iterations, keys_64)
insert_on_real_64_hash_table (number_of_iterations, keys_64)
keys_64 := generate_n_keys_64 (50 * Number_of_elements, 1000)
search_on_hash_table_64 (number_of_iterations, keys_64)
search_on_real_64_hash_table (number_of_iterations, keys_64)
print ("%NReals 64 using [0, 1] * 10000 %N")
keys_64 := generate_n_keys_64 (10 * Number_of_elements, 10000)
insert_on_hash_table_64 (number_of_iterations, keys_64)
insert_on_real_64_hash_table (number_of_iterations, keys_64)
keys_64 := generate_n_keys_64 (100 * Number_of_elements, 10000)
search_on_hash_table_64 (number_of_iterations, keys_64)
search_on_real_64_hash_table (number_of_iterations, keys_64)
print ("%NReals 64 using [0, 1] * 100000 %N")
keys_64 := generate_n_keys_64 (10 * Number_of_elements, 100000)
insert_on_hash_table_64 (number_of_iterations, keys_64)
insert_on_real_64_hash_table (number_of_iterations, keys_64)
keys_64 := generate_n_keys_64 (100 * Number_of_elements, 100000)
search_on_hash_table_64 (number_of_iterations, keys_64)
search_on_real_64_hash_table (number_of_iterations, keys_64)
print ("%NReals 64 using [0, 1] * 500000 %N")
keys_64 := generate_n_keys_64 (10 * Number_of_elements, 500000)
insert_on_hash_table_64 (number_of_iterations, keys_64)
insert_on_real_64_hash_table (number_of_iterations, keys_64)
keys_64 := generate_n_keys_64 (100 * Number_of_elements, 500000)
search_on_hash_table_64 (number_of_iterations, keys_64)
search_on_real_64_hash_table (number_of_iterations, keys_64)
-- Insert: Keys: small Reals 0..1
print ("%N%NSmall Reals 64 0..1%N")
keys_64 := generate_n_keys_range_0_1_64 (number_of_elements)
insert_on_hash_table_64 (number_of_iterations, keys_64)
insert_on_real_64_hash_table (number_of_iterations, keys_64)
search_on_hash_table_64 (number_of_iterations, keys_64)
search_on_real_64_hash_table (number_of_iterations, keys_64)
-- Insert: KeysNegative Reals
print ("%N%NNegative Reals 64%N")
keys_64 := generate_n_negative_keys_64 (10 * number_of_elements)
insert_on_hash_table_64 (number_of_iterations, keys_64)
insert_on_real_64_hash_table (number_of_iterations, keys_64)
keys_64 := generate_n_negative_keys_64 (100 * number_of_elements)
search_on_hash_table_64 (number_of_iterations, keys_64)
search_on_real_64_hash_table (number_of_iterations, keys_64)
-- Insert: Keys NAN Reals
print ("%N%NNAN Reals 64%N")
keys_64 := generate_n_nan_keys_64 (10 * number_of_elements)
insert_on_hash_table_64 (number_of_iterations, keys_64)
insert_on_real_64_hash_table (number_of_iterations, keys_64)
keys_64 := generate_n_nan_keys_64 (100 * number_of_elements)
search_on_hash_table_64 (number_of_iterations, keys_64)
search_on_real_64_hash_table (number_of_iterations, keys_64)
-- Insert: Keys Negativity Infinity Reals
print ("%N%NNegative_infinity Reals 64%N")
keys_64 := generate_n_negative_infinity_keys_64 (10 * number_of_elements)
insert_on_hash_table_64 (number_of_iterations, keys_64)
insert_on_real_64_hash_table (number_of_iterations, keys_64)
keys_64 := generate_n_negative_infinity_keys_64 (100 * number_of_elements)
search_on_hash_table_64 (number_of_iterations, keys_64)
search_on_real_64_hash_table (number_of_iterations, keys_64)
-- Insert: Keys Positive Infinity Reals
print ("%N%NPositive_infinity Reals 64%N")
keys_64 := generate_n_positive_infinity_keys_64 (10 * number_of_elements)
insert_on_hash_table_64 (number_of_iterations, keys_64)
insert_on_real_64_hash_table (number_of_iterations, keys_64)
keys_64 := generate_n_positive_infinity_keys_64 (100 * number_of_elements)
search_on_hash_table_64 (number_of_iterations, keys_64)
search_on_real_64_hash_table (number_of_iterations, keys_64)
-- -- Insert: Keys Mixed
-- print ("%N%NMixed Reals 64%N")
-- keys_64 := generate_n_mixed_keys_64 (number_of_elements)
-- insert_on_hash_table_64 (number_of_iterations, keys_64)
-- insert_on_real_64_hash_table (number_of_iterations, keys_64)
-- search_on_hash_table_64 (number_of_iterations, keys_64)
-- search_on_real_64_hash_table (number_of_iterations, keys_64)
io.readline
end
insert_on_real_32_hash_table (n: INTEGER; keys: ARRAY [REAL_32])
local
l_table: REAL_32_HASH_TABLE [INTEGER]
timer: DT_STOPWATCH
l_total_time: INTEGER_32
do
l_total_time := 0
across 1 |..| n as ic1 loop
create l_table.make (n)
create timer.make
timer.start
across keys as ic2 loop
l_table.force (generate_token, ic2.item)
end
timer.stop
l_total_time := l_total_time + timer.elapsed_time.millisecond_count
end
print ("%NTime to insert " + keys.count.out + " elements in REAL_32_HASH_TABLE [INTEGER], using the media of " + Number_of_iterations.out + " iterations is, " + (l_total_time / Number_of_iterations).truncated_to_integer_64.out + ", milliseconds")
end
search_on_real_32_hash_table (n: INTEGER; keys: ARRAY [REAL_32])
local
l_table: REAL_32_HASH_TABLE [INTEGER]
timer: DT_STOPWATCH
l_total_time: INTEGER_32
do
l_total_time := 0
create l_table.make (n)
across keys as ic2 loop
l_table.force (generate_token, ic2.item)
end
across 1 |..| n as ic loop
create timer.make
timer.start
across keys as ic2 loop
if l_table.has_key (ic2.item) then
-- found
else
-- not found
end
end
timer.stop
l_total_time := l_total_time + timer.elapsed_time.millisecond_count
end
print ("%NTime to search " + keys.count.out + " elements in REAL_32_HASH_TABLE [INTEGER], using the media of " + Number_of_iterations.out + " iterations is, " + (l_total_time / Number_of_iterations).truncated_to_integer_64.out + ", milliseconds")
end
insert_on_hash_table (n: INTEGER; keys: ARRAY [REAL_32])
local
l_table: HASH_TABLE [INTEGER, REAL_32]
timer: DT_STOPWATCH
l_total_time: INTEGER_32
do
l_total_time := 0
across 1 |..| n as ic1 loop
create l_table.make (n)
create timer.make
timer.start
across keys as ic2 loop
l_table.force (generate_token, ic2.item)
end
timer.stop
l_total_time := l_total_time + timer.elapsed_time.millisecond_count
end
print ("%NTime to insert " + keys.count.out + " elements in HASH_TABLE [INTEGER, REAL_32], using the media of " + n.out + " iterations is, " + (l_total_time / n).truncated_to_integer_64.out + ", milliseconds")
end
search_on_hash_table (n: INTEGER; keys: ARRAY [REAL_32])
local
l_table: HASH_TABLE [INTEGER, REAL_32]
timer: DT_STOPWATCH
l_total_time: INTEGER_32
do
l_total_time := 0
create l_table.make (n)
across keys as ic2 loop
l_table.force (generate_token, ic2.item)
end
across 1 |..| n as ic loop
create timer.make
timer.start
across keys as ic2 loop
if l_table.has_key (ic2.item) then
-- found
else
-- not found
end
end
timer.stop
l_total_time := l_total_time + timer.elapsed_time.millisecond_count
end
print ("%NTime to search " + keys.count.out + " elements in HASH_TABLE [INTEGER, REAL_32], using the media of " + n.out + " iterations is, " + (l_total_time / n).truncated_to_integer_64.out + ", milliseconds")
end
insert_on_real_64_hash_table (n: INTEGER; keys: ARRAY [REAL_64])
local
l_table: REAL_64_HASH_TABLE [INTEGER]
timer: DT_STOPWATCH
l_total_time: INTEGER_32
do
l_total_time := 0
across 1 |..| n as ic1 loop
create l_table.make (n)
create timer.make
timer.start
across keys as ic2 loop
l_table.force (generate_token, ic2.item)
end
timer.stop
l_total_time := l_total_time + timer.elapsed_time.millisecond_count
end
print ("%NTime to insert " + keys.count.out + " elements in REAL_64_HASH_TABLE [INTEGER], using the media of " + n.out + " iterations is, " + (l_total_time / n).truncated_to_integer_64.out + ", milliseconds")
end
insert_on_hash_table_64 (n: INTEGER; keys: ARRAY [REAL_64])
local
l_table: HASH_TABLE [INTEGER, REAL_64]
timer: DT_STOPWATCH
l_total_time: INTEGER_32
do
l_total_time := 0
across 1 |..| n as ic1 loop
create l_table.make (n)
create timer.make
timer.start
across keys as ic2 loop
l_table.force (generate_token, ic2.item)
end
timer.stop
l_total_time := l_total_time + timer.elapsed_time.millisecond_count
end
print ("%NTime to insert " + keys.count.out + " elements in HASH_TABLE [INTEGER, REAL_64], using the media of " + n.out + " iterations is, " + (l_total_time / n).truncated_to_integer_64.out + ", milliseconds")
end
search_on_hash_table_64 (n: INTEGER; keys: ARRAY [REAL_64])
local
l_table: HASH_TABLE [INTEGER, REAL_64]
timer: DT_STOPWATCH
l_total_time: INTEGER_32
do
l_total_time := 0
create l_table.make (n)
across keys as ic2 loop
l_table.force (generate_token, ic2.item)
end
across 1 |..| n as ic loop
create timer.make
timer.start
across keys as ic2 loop
if l_table.has_key (ic2.item) then
-- found
else
-- not found
end
end
timer.stop
l_total_time := l_total_time + timer.elapsed_time.millisecond_count
end
print ("%NTime to search " + keys.count.out + " elements in HASH_TABLE [INTEGER, REAL_64], using the media of " + n.out + " iterations is, " + (l_total_time / n).truncated_to_integer_64.out + ", milliseconds")
end
search_on_real_64_hash_table (n: INTEGER; keys: ARRAY [REAL_64])
local
l_table: REAL_64_HASH_TABLE [INTEGER]
timer: DT_STOPWATCH
l_total_time: INTEGER_32
do
l_total_time := 0
create l_table.make (n)
across keys as ic2 loop
l_table.force (generate_token, ic2.item)
end
across 1 |..| n as ic loop
create timer.make
timer.start
across keys as ic2 loop
if l_table.has_key (ic2.item) then
-- found
else
-- not found
end
end
timer.stop
l_total_time := l_total_time + timer.elapsed_time.millisecond_count
end
print ("%NTime to search " + keys.count.out + " elements in REAL_64_HASH_TABLE [INTEGER], using the media of " + n.out + " iterations is, " + (l_total_time / n).truncated_to_integer_64.out + ", milliseconds")
end
generate_token: INTEGER
local
random: RANDOM
do
create random.make
random.set_seed (generate_seed)
random.start
Result := random.item
end
generate_n_keys_range_0_1 (n: INTEGER): ARRAY [REAL_32]
local
random: RANDOM
do
create random.make
random.set_seed (generate_seed)
create Result.make_filled (0.0, 1, n)
across 1 |..| n as ic loop
random.forth
Result [ic.item] := (random.real_item)
end
end
generate_n_keys_range_0_1_64 (n: INTEGER): ARRAY [REAL_64]
local
random: RANDOM
do
create random.make
random.set_seed (generate_seed)
create Result.make_filled (0.0, 1, n)
across 1 |..| n as ic loop
random.forth
Result [ic.item] := (random.double_item)
end
end
generate_n_large_keys (n: INTEGER): ARRAY [REAL_32]
local
random: RANDOM
const: REAL_32
do
const := 3.0
create random.make
random.set_seed (generate_seed)
create Result.make_filled (0.0, 1, n)
across 1 |..| n as ic loop
random.forth
Result [ic.item] := ((random.real_item) * const.power (38)).truncated_to_real
end
end
generate_n_large_keys_64 (n: INTEGER): ARRAY [REAL_64]
local
random: RANDOM
const: REAL_64
do
const := 1.0
create random.make
create Result.make_filled (0.0, 1, n)
across 1 |..| n as ic loop
random.forth
Result [ic.item] := (random.double_item) * const.power (308)
end
end
generate_n_keys (n: INTEGER; range: INTEGER): ARRAY [REAL_32]
local
random: RANDOM
do
create random.make
random.set_seed (generate_seed)
create Result.make_filled (0.0, 1, n)
across 1 |..| n as ic loop
random.forth
Result [ic.item] := (random.real_item) * range
end
end
generate_n_keys_64 (n: INTEGER; range: INTEGER): ARRAY [REAL_64]
local
random: RANDOM
do
create random.make
random.set_seed (generate_seed)
create Result.make_filled (0.0, 1, n)
across 1 |..| n as ic loop
random.forth
Result [ic.item] := (random.double_item) * range
end
end
generate_n_nan_keys (n: INTEGER): ARRAY [REAL_32]
do
create Result.make_filled ({REAL_32}.nan, 1, n)
end
generate_n_nan_keys_64 (n: INTEGER): ARRAY [REAL_64]
do
create Result.make_filled ({REAL_64}.nan, 1, n)
end
generate_n_negative_infinity_keys (n: INTEGER): ARRAY [REAL_32]
do
create Result.make_filled ({REAL_32}.negative_infinity, 1, n)
end
generate_n_negative_infinity_keys_64 (n: INTEGER): ARRAY [REAL_64]
do
create Result.make_filled ({REAL_64}.negative_infinity, 1, n)
end
generate_n_positive_infinity_keys (n: INTEGER): ARRAY [REAL_32]
do
create Result.make_filled ({REAL_32}.positive_infinity, 1, n)
end
generate_n_positive_infinity_keys_64 (n: INTEGER): ARRAY [REAL_64]
do
create Result.make_filled ({REAL_64}.positive_infinity, 1, n)
end
generate_n_negative_keys (n: INTEGER): ARRAY [REAL_32]
local
random: RANDOM
do
create random.make
random.set_seed (generate_seed)
create Result.make_filled (0.0, 1, n)
across 1 |..| n as ic loop
random.forth
Result [ic.item] := - (random.item.to_real) * 1000
end
end
generate_n_negative_keys_64 (n: INTEGER): ARRAY [REAL_64]
local
random: RANDOM
do
create random.make
random.set_seed (generate_seed)
create Result.make_filled (0.0, 1, n)
across 1 |..| n as ic loop
random.forth
Result [ic.item] := - (random.item.to_double) * 1000
end
end
generate_n_mixed_keys (n: INTEGER): ARRAY [REAL_32]
local
random: RANDOM
val: INTEGER
do
create random.make
create Result.make_filled (0.0, 1, n)
across 1 |..| n as ic loop
random.forth
val := random.item \\ 5
inspect val
when 0 then
random.forth
Result [ic.item] := (random.item.to_real)
when 1 then
Result [ic.item] := {REAL_32}.nan
when 2 then
Result [ic.item] := {REAL_32}.positive_infinity
when 3 then
random.forth
Result [ic.item] := {REAL_32}.negative_infinity
when 4 then
Result [ic.item] := - (random.real_item) * 1000
end
end
end
generate_n_mixed_keys_64 (n: INTEGER): ARRAY [REAL_64]
local
random: RANDOM
val: INTEGER
do
create random.make
create Result.make_filled (0.0, 1, n)
across 1 |..| n as ic loop
random.forth
val := random.item \\ 5
inspect val
when 0 then
random.forth
Result [ic.item] := (random.item.to_double)
when 1 then
Result [ic.item] := {REAL_64}.nan
when 2 then
Result [ic.item] := {REAL_64}.positive_infinity
when 3 then
random.forth
Result [ic.item] := {REAL_64}.negative_infinity
when 4 then
Result [ic.item] := - (random.item.to_double) * 1000
end
end
end
generate_custom_large_reals (n: INTEGER; a_real: REAL_32; steps: REAL_32): ARRAY [REAL_32]
do
create Result.make_filled (0.0, 1, n)
across 1 |..| n as ic loop
Result [ic.item] := a_real + steps*ic.item
end
end
feature -- Constants
generate_seed: INTEGER
local
l_time: TIME
do
create l_time.make_now
Result := l_time.hour
Result := Result * 60 + l_time.minute
Result := Result * 60 + l_time.second
Result := Result * 1000 + l_time.milli_second
Result := Result * 10000 + l_time.nano_second
end
Number_of_elements: INTEGER = 10000
Number_of_iterations: INTEGER = 50
end
@jvelilla
Copy link
Author

Updated code to compare REAL_[32|64]_HASH_TABLE , with HASH_TABLE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment