Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

First step of Performance Tuning

823 views

Published on

YAPC::Kansai 2017 Osaka トーク資料
http://yapcjapan.org/2017kansai/talks.html#/detail/13

Published in: Technology
  • 念のため自分で指摘入れておきますが、 s/メンテナンシビリティ/メンテナビリティ/g です
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

First step of Performance Tuning

  1. 1. by risou at YAPC::Kansai 2017 Osaka
  2. 2. ▸ risou ▸ ID "risouf" ▸ ▸ 🤔 ▸ ▸ 🙏
  3. 3. ▸ ▸ Web ▸ ▸ ▸
  4. 4. ▸ 10 ▸ 10 …… ▸ ▸ 10 5
  5. 5. ▸ A 3h B 1h C 0.5h D 1h E 2h F 1h G 4h H 2h
  6. 6. ▸ A 3h B 1h C 0.5h D 1h E 2h F 1h G 4h H 2h
  7. 7. ▸ ▸ 
 ▸
  8. 8. ▸ ▸ ▸ ▸ Web
  9. 9. ▸ ▸ ▸ ▸ ▸ 1
  10. 10. ▸ my @primes = (); for my $num (2 .. 100_000) { if ($num == 2) { push @primes, $num; next; } my $is_prime = 1; for (2 .. $num - 1) { if ($num % $_ == 0) { $is_prime = 0; } } if ($is_prime == 1) { push @primes, $num; } }
  11. 11. ▸ 366.103 sec ▸ oh... ▸ 10 6 ▸
  12. 12. ▸ ▸ 10 ▸ 50
  13. 13. ▸ ▸ ▸ ▸ 
 1 2 - 1 

  14. 14. ▸ 
 my @primes = (); for my $num (2 .. 100_000) { if ($num == 2) { push @primes, $num; next; } my $is_prime = 1; for (2 .. $num - 1) { if ($num % $_ == 0) { $is_prime = 0; last; } } if ($is_prime == 1) { push @primes, $num; } }
  15. 15. ▸ 31.404 sec ▸ 1/10 ▸ 5 ▸ 2 
 25 ▸
  16. 16. ▸ my @primes = (); for my $num (2 .. 100_000) { if ($num == 2) { push @primes, $num; next; } my $is_prime = 1; for (@primes) { if ($num % $_ == 0) { $is_prime = 0; last; } } if ($is_prime == 1) { push @primes, $num; } }
  17. 17. ▸ ▸ ▸ 4 = 2 x 2 60 = 2 x 2 x 3 x 5 ▸ 4 2 
 60 2 3 5
  18. 18. ▸ @primes ▸ ▸ 
 @primes ▸ 3.191 sec ▸ 1/10
  19. 19. ▸ …… ▸ ▸ ▸ 91 7 13 ▸ 91 13 ▸ 91 13
  20. 20. ▸ N 
 √N ▸ 91 9.53... ▸ 2 9 10 90
  21. 21. ▸ √N my @primes = (); for my $num (2 .. 100_000) { if ($num == 2) { push @primes, $num; next; } my $is_prime = 1; for (@primes) { last if $_ > sqrt($num); if ($num % $_ == 0) { $is_prime = 0; last; } } if ($is_prime == 1) { push @primes, $num; } }
  22. 22. ▸ 0.126 sec ▸ 1 ▸ 1/3000
  23. 23. …… ▸ my @primes = (); for my $num (2 .. 100_000) { if ($num == 2) { push @primes, $num; next; } my $is_prime = 1; for (@primes) { last if $_ > sqrt($num); if ($num % $_ == 0) { $is_prime = 0; last; } } if ($is_prime == 1) { push @primes, $num; } }
  24. 24. ▸ 2 ▸ 2 ▸ 2 
 ▸
  25. 25. ▸ my @numbers = (1) x 100_001; $numbers[0] = 0; $numbers[1] = 0; for my $num (2 .. sqrt(100_000)) { if ($numbers[$num]) { for (2 .. (100_000 / $num)) { $numbers[$num * $_] = 0; } } }
  26. 26. ▸ 0.026 sec ▸ …… ▸ ▸ ▸ ▸ 

  27. 27. ▸ ▸ ▸ ▸ ▸ 

  28. 28. ▸ memoization ( not memonization ) ▸ 
 ▸ ▸ 

  29. 29. ▸ # normal sub fib { my $number = shift; return 1 if $number == 1 || $number == 2; return fib($number - 1) + fib($number - 2); } # memoize my %memo; sub fib { my $number = shift; return 1 if $number == 1 || $number == 2; return $memo{$number} if $memo{$number}; $memo{$number} = fib($number - 1) + fib($number - 2); return $memo{$number}; }
  30. 30. ▸ 30 ▸ normal: 545.545 msec / memoize: 0.114 msec ▸ ▸ is_prime ▸ is_prime 

  31. 31. ▸ Perl ▸ Memoise ▸ normal memoize('fib'); ▸ ▸ ▸ Memoise 30 18.654 msec
  32. 32. ▸ ▸ ▸ ▸ Memoise
  33. 33. List::Compare ▸ 2 ▸ ▸
  34. 34. List::Compare ▸ 10 ▸ ▸ List::Compare get_Lonly
  35. 35. List::Compare ▸ List::Compare # List::Compare my $lc = List::Compare->new(@primes, @fibs); my @left_only = $lc->get_Lonly; # normal my %exists; %exists = map { $_ => 1 } @primes; for (@fibs) { $exists{$_} = 0 if ($exists{$_}); } my @left_only = grep { $exists{$_} == 1 } keys %exists;
  36. 36. List::Compare ▸ List::Compare: 35.733 msec / normal: 17.769 msec ▸ List::Compare 
 1 List::Compare ▸ List::Compare new
  37. 37. ▸ 
 ▸ 
 ▸ 

  38. 38. ▸ 1 ▸ push join 
 # push -> join my @str; for (1 .. 1_000_000) { push @str, "The quick brown fox jumps over the lazy dog."; } my $result = join "", @str; # concat my $result; for (1 .. 1_000_000) { $result .= "The quick brown fox jumps over the lazy dog."; }
  39. 39. ▸ ▸ 
 # multiple 2, 4 times my @numbers; for (1 .. 100_000_000) { push @numbers, $_ * 2 * 2 * 2 * 2; } # multiple 16, 1 time my @numbers; for (1 .. 100_000_000) { push @numbers, $_ * 16; }
  40. 40. ▸ ▸ # function in loop sub sum { my ($x, $y) = @_; return $x + $y; } my $total = 0; for (1 .. 1_000_000) { $total = sum($total, $_); } # loop in function sub sum_list { my ($list) = @_; my $result; for (@$list) { $result += $_; } return $result; } my $total = sum_list([1 .. 1_000_000]);
  41. 41. ▸ Function in Loop: 360.701 msec ▸ Loop in Function: 94.884 msec ▸ 
 Function in Loop ▸
  42. 42. Struct of Arrays ▸ ▸ Perl ▸ Struct of Arrays (SoA) Array of Structs (AoS) ▸ AoS ▸ 
 SoA
  43. 43. Struct of Arrays ▸ SoA AoS Clone # Array of Structs my $aos = []; for (1 .. 100_000) { push @$aos, { number => $_, double => $_ * 2 }; } my $copy = clone($aos); # Struct of Arrays my $soa = {}; my @numbers; my @doubles; my $count; for (1 .. 100_000) { push @numbers, $_; push @doubles, $_ * 2; } $soa = { number => @numbers, double => @doubles }; my $copy = clone($soa);
  44. 44. Struct of Arrays ▸ AoS: 640.302 msec / SoA: 215.323 msec ▸ SoA Clone ▸ ▸ AoS: 24,800,144 bytes / SoA: 6,400,424 bytes
  45. 45. ▸ ▸ …… ▸ ▸ DB …… ▸ ▸
  46. 46. ▸ ▸ 
 ▸ NYTProf
  47. 47. ▸ DB ▸ ▸ ▸ DB/KVS ▸ ▸ ▸ CDN
  48. 48. ▸ DB ▸ ▸ ▸ DB 
 ▸
  49. 49. ▸ ▸ ▸ ▸ ▸ ▸ ▸
  50. 50. ▸ ▸ ▸ ▸
  51. 51. ▸ ▸ ▸ ▸ ▸ ▸ ▸

×