并发需求下Scala和Erlang的比较-QConBeijing2010
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share

并发需求下Scala和Erlang的比较-QConBeijing2010

  • 3,574 views
Uploaded on

 

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
3,574
On Slideshare
3,554
From Embeds
20
Number of Embeds
1

Actions

Shares
Downloads
94
Comments
0
Likes
10

Embeds 20

http://www.slideshare.net 20

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Scala Erlang Beijing 2010.04 Caoyuan (NetBeans Dream Team Member) http://blogtrader.net @dcaoyuan
  • 2. •  ( ) •  • 
  • 3. •  •  CPU •  •  •  •  • 
  • 4. - •  1206 •  103 x1.5 =155 6 •  6144 CPU 5120 GPU •  •  1280 •  1280 •  3 16 •  CPU 60%
  • 5. •  •  Fortress Erlang Scala => Scala •  •  •  DSL •  - •  Map-Reduce, Fork-Join • 
  • 6. Actor Erlang Scala Erlang Scala Actor •  Erlang •  Erlang VM Actor •  •  Scala •  Actor JVM •  actor
  • 7. Actor - Actor Actor •  Actors actor ! ChangeYourPosition Pid ! change_your_position •  Actors val a = actor { loop(I) -> def loop(i: Int): Unit = receive react { I -> I, loop(I + 1); case i: Int => loop(i + 1) exit -> io:format(“~p~n”, [I]) case Exit => println(i) end } end loop(0) Pid = spawn(fun loop/1, [0]) } •  •  Actor •  loop (Scala Erlang) (Scala) •  Actors
  • 8. Actor Actor OO •  •  OO •  Actor •  (immutable) •  •  •  Actor •  Actor Actor •  Actor Actor Actor
  • 9. Actor Actor •  •  •  Actor CPU • 
  • 10. Actor CPU
  • 11. Actor – vs Actor Actor CPU Actors CPU
  • 12. JVM Scheduler •  •  IO •  CPU
  • 13. Scala JVM Actor Scheduler •  Actor Object •  Actor •  Actor •  Actor Actor •  Actors Actors •  Scala IO IO Actor
  • 14. Erlang Actor Process Scheduler •  Erlang Actor Process •  Process •  Process •  Process •  Erlang IO Process IO
  • 15. •  Actor   Actors Actor •    sleep IO   •  Scala Actor NIO
  • 16. Erlang vs Scala – •  Erlang Process •  Process •  Process •  Process CPU Process •  log   2-core 140 process 200 process CPU 1 / 200 * 2 Core = 1%   8-core 700 process 980 process CPU 1 / 980 * 8 Core = 0.82%
  • 17. Erlang vs Scala – •  Scala Actor •  Actor Actors •  Actor •  •  Actor
  • 18. Erlang vs Scala – •  Erlang Process •  •  (GC ) •  ( ) •  VM ( ) •  •  ( )
  • 19. Erlang vs Scala – •  Scala Actor Erlang •  •  •  VM VM •  •  JVM • 
  • 20. Erlang vs Scala – object ThreadRing { val (nTokens, nProcs) = (20000, 10000) def main(args: Array[String]) { val last= Ring(null, 1) val h = Range(nProcs, 2, -1).foldLeft(last){(acc, i) => Ring(acc, i)} last.next = h h ! nTokens } case class Ring(var next: Ring, id: Int) extends Actor { start def act = loop { react { case 1 => println("Done") println(id) System.exit(0) case token: Int => next ! token - 1 println(token) //fib(N_FIB) } } } }
  • 21. Erlang vs Scala – -module(threadring). -export([main/0, ring/2]). -define(N_TOKENS, 20000). -define(N_PROCS, 10000). main() -> start(?N_TOKENS). start(NToken) -> H = lists:foldl(fun(Id, Pid) -> spawn(threadring, ring, [Id, Pid]) end, self(), lists:seq(?N_PROCS, 2, -1)), H ! NToken, ring(1, H). ring(Id, Pid) -> receive 1 -> io:fwrite(”~p~n", [done]), io:fwrite("~b~n", [Id]), erlang:halt(); Token -> Pid ! Token - 1, io:fwrite("~b~n", [Token]), //fib(?N_FIB), ring(Id, Pid) end.
  • 22. Erlang vs Scala –FP OO+FP •  Erlang •  •  Process • 
  • 23. Erlang vs Scala –FP OO+FP •  Scala OO+FP •  •  Actor Actors •  Actor • 
  • 24. Q&A Scala