JysonFaq » History » Version 2
Alan Kennedy, 2009-03-21 12:23 PM
1 | 1 | Alan Kennedy | h1. Jyson FAQ |
---|---|---|---|
2 | |||
3 | {{toc}} |
||
4 | |||
5 | h2. Why use Jyson instead of a java codec? |
||
6 | |||
7 | The advantage of using jyson instead of a java codec is that jyson produces a hierarchy of jython objects, namely "org.python.core.PyObject":http://www.jython.org/docs/javadoc/org/python/core/PyObject.html . These objects are custom designed to be manipulated by jython code. |
||
8 | |||
9 | On the other hand, a java codec might produce a "java.util.Map":http://java.sun.com/j2se/1.5.0/docs/api/java/util/Map.html or "java.util.List":http://java.sun.com/j2se/1.5.0/docs/api/java/util/List.html or other java objects, which it is possible to manipulate from jython code, but would not be natural jython objects. |
||
10 | |||
11 | Consider the following |
||
12 | |||
13 | <pre> |
||
14 | Jython 2.5b2+ (trunk, Mar 8 2009, 20:40:45) |
||
15 | [Java HotSpot(TM) Client VM (Sun Microsystems Inc.)] on java1.5.0_17 |
||
16 | Type "help", "copyright", "credits" or "license" for more information. |
||
17 | >>> from com.xhaus.jyson import JysonCodec |
||
18 | >>> l = JysonCodec.loads('[3,1,4,1,5,9,2,7]') |
||
19 | >>> type(l) |
||
20 | <type 'list'> |
||
21 | >>> l |
||
22 | [3, 1, 4, 1, 5, 9, 2, 7] |
||
23 | >>> l.count(1) |
||
24 | 2 |
||
25 | >>> import java |
||
26 | >>> jl = java.util.ArrayList([3,1,4,1,5,9,2,7]) |
||
27 | >>> type(jl) |
||
28 | <type 'java.util.ArrayList'> |
||
29 | >>> jl |
||
30 | [3, 1, 4, 1, 5, 9, 2, 7] |
||
31 | >>> jl.count(1) |
||
32 | Traceback (most recent call last): |
||
33 | File "<stdin>", line 1, in <module> |
||
34 | AttributeError: 'java.util.ArrayList' object has no attribute 'count' |
||
35 | >>> |
||
36 | </pre> |
||
37 | |||
38 | h2. Why use jyson instead of a python codec? |
||
39 | |||
40 | 2 | Alan Kennedy | Simple answer: speed and resource efficiency. |
41 | 1 | Alan Kennedy | |
42 | When you run a pure python program under jython, there is an "interpretive overhead":http://en.wikipedia.org/wiki/Interpreter_(computing)#Efficiency which makes it likely that the pure python code will run slower, and use more memory, than the equivalent code written in java (although "advances in JVM optimization":http://blogs.sun.com/jrose/entry/bravo_for_the_dynamic_runtime mean that this performance difference is not guaranteed). |
||
43 | |||
44 | Jyson is written in pure java, and is thus highly likely to be significantly faster and more resource efficient than a pure python codec. |