Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Python 2.7 on Ubuntu:

  $ python -mtimeit -s "from json import dumps; 
  d = {
      'foo': 'bar',
      'food': 'barf',
      'good': 'bars',
      'dood': 'wheres your car?',
      'wheres your car': 'dude?',
  }
  " "dumps(d)"
  100000 loops, best of 3: 6.89 usec per loop

  $ python -mtimeit -s "from cPickle import dumps; 
  d = {
      'foo': 'bar',
      'food': 'barf',
      'good': 'bars',
      'dood': 'wheres your car?',
      'wheres your car': 'dude?',
  }
  " "dumps(d)"
  100000 loops, best of 3: 7.78 usec per loop
So `json` seems faster than `cPickle`. Right? Wrong!:

  $ python -mtimeit -s "from cPickle import dumps; 
  d = {
      'foo': 'bar',
      'food': 'barf',
      'good': 'bars',
      'dood': 'wheres your car?',
      'wheres your car': 'dude?',
  }
  " "dumps(d, -1)"
  100000 loops, best of 3: 3.59 usec per loop


What does the "-1" dump argument mean in 3rd example?


The equivalent of using pickle.HIGHEST_PROTOCOL or pickle.dumps(obj, protocol=pickle.HIGHEST_PROTOCOL) -- any negative value will force highest protocol.


It says to use the highest optimization level.


No. It says use the highest available protocol.

The highest available protocol is not necessarily the fastest. There might be other than pure speed reasons to introduce the next Pickle protocol.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: