Skip to content

Instantly share code, notes, and snippets.

@meoooh
Created December 21, 2016 14:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meoooh/8686d254b05d0a2ed2ccb635bf00b6d2 to your computer and use it in GitHub Desktop.
Save meoooh/8686d254b05d0a2ed2ccb635bf00b6d2 to your computer and use it in GitHub Desktop.
hhash = {}
(0..10000000).each{ |i| hhash[i] = rand };
parallel_sum = Parallel.map(hhash.each_slice(1000)) do |ele| # https://github.com/grosser/parallel
ele.sum{|value| value.last}
end.sum
puts parallel_sum
normal_sum = hhash.sum{|_,v| v}
puts normal_sum
parallel_sum == normal_sum # false
hhash = {}
(0..10000000).each{ |i| hhash[i] = 1 };
parallel_sum = Parallel.map(hhash.each_slice(1000)) do |ele| # https://github.com/grosser/parallel
ele.sum{|value| value.last}
end.sum
puts parallel_sum
normal_sum = hhash.sum{|_,v| v}
puts normal_sum
parallel_sum == normal_sum # true
@meoooh
Copy link
Author

meoooh commented Dec 21, 2016

추가적으로



hhash = {}
(0..10000000).each{ |i| hhash[i] = 0.3 };

parallel_sum = Parallel.map(hhash.each_slice(1000)) do |ele|
  ele.sum{|value| value.last}
end.sum

puts parallel_sum

normal_sum = hhash.sum{|_,v| v}

puts normal_sum

parallel_sum == normal_sum # false


hhash = {}
(0..10000000).each{ |i| hhash[i] = 0.5 };

parallel_sum = Parallel.map(hhash.each_slice(1000)) do |ele|
  ele.sum{|value| value.last}
end.sum

puts parallel_sum

normal_sum = hhash.sum{|_,v| v}

puts normal_sum

parallel_sum == normal_sum # true

위와 같은 현상이 있네요.

1/3 = 0.33333333(무한소수) 를 sum하면 두가지 방식의 sum 값이 다르고
1/2 = 0.5를 sum하면 두가지 방식의 sum의 값이 같네요.

@wagurano
Copy link

require 'parallel'
require 'active_support/all'

따라서 해보실 분은 위와 같이 코드를 추가해보세요.
RORLAB 슬랙 채널 http://rorlab.slackarchive.io/question_answer/-/1479210726.000646/1482365225.000055/1482365225000055/ 에서 이야기 하고 있습니다. 빅넘버와 래셔널을 참고하세요.

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