Python’s Valentines Day Gift to Clojure

Inspired by meh’s Ruby-Clj module, I created the python equivalent “pyclj” last weekend. Pyclj is a clojure literal reader/writer for python. It enables data exchange between python and clojure, in a clojure-native way. It’s Valentines Day today, I’d like to release it as the gift of python to clojure :)

The API is very simple. It’s all like python’s data modules (json, pickle)

import clj

clj.loads("[1 2 3]")
clj.dumps({"a":1, "b":2})

Clojure types are mapping to python data structures :

Clojure Python
list list
vector list
set set
map dict
nil None
string string
int int
float float
boolean boolean
char string
keyword string

But how we win clojure’s heart from ruby?

We are faster.

Considering clojure literal below:

[1 2 3 true false nil {:a 21.3 :b 43.2} "Hello"]

Comparing ruby-clj(0.0.4.5, ruby 1.9.3p0) and pyclj(0.1.3 python 2.7.2):

require 'clj'

s = "[1 2 3 true false nil {:a 21.3 :b 43.2} \"Hello\"]"

t1 = Time.now()
for i in 0...10000
  Clojure.parse(s)
end
puts Time.now()-t1
import clj
import time

s = "[1 2 3 true false nil {:a 21.3 :b 43.2} \"Hello\"]"

t1 = time.time()
for i in range(10000):
  clj.loads(s)
print time.time()-t1

The result:
ruby: 13.451157809
python: 0.712423086166
Edit 20120216 13:30
ruby-clj 0.0.5.3 has resolved the performance issue :)
The new result ruby-clj/0.0.5.4 Vs pyclj0.1.4 (on my laptop):
ruby-clj: 2.044872364
pyclj: 1.19659209251

The project is hosted on github. Feel free to join the development and enhance it.

9 thoughts on “Python’s Valentines Day Gift to Clojure

  1. Well, your implementation misses some checks, I opened an issue on github for it.

    > test/bench/bench.rb
    1.613363001
    > test/bench/bench.py
    1.01751899719

    I got it down to that, if you want to update the bench results running on your machine :)

    • The bug has been fixed. Thank you for your information.

      It’s great to see ruby-clj’s performance boost.
      I will add some notes to the posts to indicate that :)

  2. Pingback: Wed, 15 Feb 2012 13:27:50 | savvykang UNITED STATES PHP

  3. Pingback: parsing project.clj – take 2 | eugecm UNITED STATES PHP

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>