Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

May I create a Template instance first and the use it to render context? #4

Closed
childe opened this issue May 7, 2015 · 5 comments
Closed

Comments

@childe
Copy link

childe commented May 7, 2015

In http://product.hubspot.com/blog/jinjava-a-jinja-for-your-java the author says speed of jinja2 in python is 1387.3108 runs/s which is a bit slower than jinjava.

But I compared the 2 projects:
jinja2 is much faster if a Template is prepared in advance.

python code:

from jinja2 import Template

n = 70000
event = {'name': "jia.liu"}
t = Template('Hello, {% if name is defined %} {{name}} {% else %} world {% endif %}')
msg = []
for i in range(n):
    msg.append(t.render(**event))

jinjava code:

Jinjava jinjava = new Jinjava();

Map<String, Object> context = new HashMap<>();      
context.put("name", "Jared");
String template = "Hello, {% if name is defined %} {{name}} {% else %} world {% endif %}";

for (int i = 0; i < 70000; i++) {

    jinjava.renderForResult(template, context);

}       
System.out.println(System.currentTimeMillis()-s);

I really hope i use jinjava in a wrong way. Is there any method I could first create a Template instance like jinja2?

@childe
Copy link
Author

childe commented May 7, 2015

In my test running at macbook pro I5 2.6G, python code runs in 0.9s, and java 2.5s.

@jaredstehler
Copy link
Contributor

You can create a parsed representation of a template using the interpreter instance, as follows:

    JinjavaInterpreter interpreter = new JinjavaInterpreter(jinjava, jinjava.getGlobalContext(), jinjava.getGlobalConfig());
    Node parsedTemplate = interpreter.parse(template);

    s = System.currentTimeMillis();

    for (int i = 0; i < 70000; i++) {
      interpreter.render(parsedTemplate, true);
    }

This actually runs in 1/3 of the time of your other java example, which is promising! 😄 I'm going to create a nicer factory method of getting a new interpreter instance from a Jinjava object.

@jaredstehler
Copy link
Contributor

Results of updated benchmark code with precompiled (e6dbd9d):

Result: 148.433 ±(99.9%) 2.078 ops/s [Average]
  Statistics: (min, avg, max) = (77.945, 148.433, 158.897), stdev = 8.798
  Confidence interval (99.9%): [146.355, 150.511]


# Run complete. Total time: 00:27:09

Benchmark                                                Mode  Samples     Score     Error  Units
c.h.j.b.jinja2.Jinja2Benchmark.precompiledBenchmark     thrpt      200  9951.372 ± 203.122  ops/s
c.h.j.b.jinja2.Jinja2Benchmark.realWorldishBenchmark    thrpt      200  1160.294 ±  15.803  ops/s
c.h.j.b.liquid.LiquidBenchmark.parse                    thrpt      200  1657.029 ±  23.344  ops/s
c.h.j.b.liquid.LiquidBenchmark.parseAndRender           thrpt      200   148.433 ±   2.078  ops/s

@childe
Copy link
Author

childe commented May 8, 2015

Yes, it is much faster than before~
But our java version is 1.7 unfortunately, so i have to use jinjava 1.0.9. Maybe that is why is runs 1/2 of the time of previous example.
Thank you all very much!

@childe
Copy link
Author

childe commented May 13, 2015

before I could create Node , I need a interpreter , but context of the interpreter varies every time, so use below method is better:

        TokenParser t =  new TokenParser(null,template);
        Node parsedTemplate =TreeParser.parseTree(t);
        String s = interpreter.render(parsedTemplate);

anev added a commit to anev/jinjava that referenced this issue May 12, 2016
Now {{ theString[1:4] }} will render as a substring.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants