Hacker News new | ask | show | jobs
by kirubakaran 6652 days ago
I tried profiling these two functions:

  import cProfile
  
  def flipbits():
      c=0
      for i in range(1,400000):
          b=~i
          c=c+b
      print c

  def negnums():
      c=0
      for i in range(1,400000):
          b=-i
          c=c+b
      print c

  cProfile.run("flipbits()")
140 function calls in 0.175 CPU seconds

  cProfile.run("negnums()")
140 function calls in 0.176 CPU seconds

---

Summary:

Flipping bits is tiny bit faster actually.To make negnums function produce the same result as flipbits, we need to change b=-i to b=-i-1 and this is even slower. Poke holes in my experiment please.