Monday, September 21, 2015

Operator Overloading Benchmark


JavaScript has no support for operator overloading, and this is generally a good thing; because in languages that do support it, programmers often abuse it and create code that is harder to read. The only real exception is with math libraries, where having operator overloading for vectors, matrices, and complex number classes can be very helpful, and make code more readable.

Emulation of operator overloading in JavaScript has a very high performance overhead, so much so that it is simply unusable if used traditionally with no extra syntax. Both Rusthon and RapydScript, after careful consideration, have adopted many of the same optimization patterns: and first is no direct support for operator overloading.

Brython, on the other hand, has attempted to fully support all of the dynamic features of Python, including operator overloading. As you will see in this benchmark, the price is very fucking high, Brython is 50 times slower than regular CPython3, and 295 times slower than Rusthon.

operator_overloading.py

The Brython FAQ states that Brython is "somewhere between 3 to 5 times slower", nice try guys, that should be updated to say something like: "Brython is a toy, and can not actually be used for anything".

with oo:

def benchmark(n):
 a = [ Vector(i*0.09,i*0.05, i*0.01) for i in range(n)]
 b = [ Vector(i*0.08,i*0.04, i*0.02) for i in range(n)]
 c = []
 d = []
 for j in range(n):
  with oo:
   u = a[j]
   v = b[j]
   c.append( u+v )
   d.append( u*v )
 return [c,d]

Above is a snippet of code from the benchmark, the special syntax with oo: enables operator overloading for just those statements under it in Rusthon. If you hate this syntax, you can also use with operator_overloading:, or fore go operator overloading and directly use the method names like this:

def benchmark(n):
 a = [ Vector(i*0.09,i*0.05, i*0.01) for i in range(n)]
 b = [ Vector(i*0.08,i*0.04, i*0.02) for i in range(n)]
 c = []
 d = []
 for j in range(n):
  u = a[j]
  v = b[j]
  c.append( u.__add__(v) )
  d.append( u.__mul__(v) )
 return [c,d]

No comments:

Post a Comment