require 'benchmark' require 'numo/narray' require 'bindata' class PGDataFloatArray < BinData::Record array :numbers do uint32be value: 8 double_be :pg_data end end n = 1000 rand_data = 10000.times.to_a.map { rand() }; Benchmark.bmbm do |x| x.report("PostgreSQL String:") do n.times do result_array = "{#{rand_data.join(',')}}" end end x.report("Binary pack:") do n.times do result_array = rand_data.pack("G*"); end end x.report("Numo-NArray to_bytes:") do numo_array = Numo::DFloat[rand_data] n.times do result_array = numo_array.to_network.to_binary end end # x.report("Binary pack with join:") do # n.times do # pg_array = PGDataFloatArray.new() # data = {pg_data: 0} # rand_data.each { |v| data[:pg_data] = v; pg_array.numbers << data } # # result_array = pg_array.to_binary_s # end # end x.report(".pack() then string insert:") do n.times do insert_key = [8].pack("N"); result_array = rand_data.pack("G*"); rand_data.size.times do |i| result_array.insert(i*12, insert_key) end end end x.report(".map(.pack).join()") do n.times do join_sep = [8].pack("N"); pack_arr = [0] result_array = rand_data.map { |v| pack_arr[0] = v; pack_arr.pack("G") }.join(join_sep) end end x.report("Prefilled string slicing") do n.times do prep_string = [8].pack("Nx8") * rand_data.length pack_str = "G" pack_array = [0] pack_buffer = ' ' pack_opts = { buffer: pack_buffer } rand_data.each_index do |index| pack_array[0] = rand_data[index] prep_string[12*index + 4, 8] = pack_array.pack(pack_str) end end end end