java模版引擎freemarker和velocity的对比

**

一、是否维护

**
1.Freemarker
在freemarker的官网中看到,fm在2015-7-1后就托管给apache基金会了,后面的bug修改都来自于git。最近的一次更新在2016年6月,顺便值得一提的是,这个版本是最后一个版本了。
Around 2015-07-01, FreeMarker was voted in into the Apache Incubator, and the project (all code from which the releases and the Web site are created, along with the right for using the “FreeMarker” product name) was granted to the Apache Software Foundation by the earlier owners. The license remains Apache License, Version 2.0. In 2015-09-02, the main code base was imported from GitHub into the Apache Software Foundation infrastructure, where development continues.

2.Velocity
Velocity早在几年前的时候就已经托管给apache了,在git上我们可以看到,最新的更新是2010年的11月,说明velocity很多最新的bug已经没有人处理了,

当然我们一定要用的话,还是有很多大神对velocity的bug进行了修改并提交了git,所以实质上我们要用的话,可以对git上的版本进行打包到我们的私服上。当然,如果大神改出了新bug也是有可能的。

**

二、性能对比。

**
1.针对10W条数据的模版生成对比
生成的时间单位都是ms,fm用是2.3.25-incubating,vm用的是1.7
Fm vm
1 195 229
2 121 223
3 122 221
4 120 733
5 120 232
6 150 396
7 150 326
8 273 198
平均
可以从数据中很轻易的看出fm的性能和稳定性都比vm好。
下面是测试的代码
vm:

public void test(){
        VelocityEngine engine = new VelocityEngine();
        Properties p = new Properties();
        p.setProperty(VelocityEngine.RESOURCE_LOADER,"class");
        p.setProperty("class.resource.loader.class","org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        engine.init(p);

        Template template = engine.getTemplate("vm/test.vm","UTF-8");

        VelocityContext context = new VelocityContext();

        context.put("name","陈善奔");
        context.put("age","18");
        List list = Lists.newArrayList();
        for (int i = 0; i < 100000; i++) {
            list.add(UUID.randomUUID().toString());
        }
        context.put("list",list);

        long one = System.currentTimeMillis();
        StringWriter writer = new StringWriter();
        System.out.println(one);
        template.merge(context,writer);
        System.out.println(System.currentTimeMillis() - one);
        //System.out.println(writer.toString());
    }

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>测试title>

head>

<body>
<div class="main">
    <h2>测试h2>
    <div>
        ${name},年龄${age}
        #foreach($uuid in ${list})
            <div>${uuid}div>
        #end
    div>
div>
body>

html>  

freemarker的代码:

public void test() throws TemplateException {
        Configuration cfg = new Configuration();
        ClassTemplateLoader ctl = new ClassTemplateLoader(getClass(),"/ftl");
//        cfg.setDirectoryForTemplateLoading("ftl/");
        TemplateLoader[] loaders = new TemplateLoader[]{ctl};
        MultiTemplateLoader mtl = new MultiTemplateLoader(loaders);
        cfg.setTemplateLoader(mtl);
        cfg.setEncoding(Locale.CHINA,"utf-8");
        cfg.setDefaultEncoding("utf-8");

        Map context = new HashMap<>();
        context.put("name","陈善奔");
        context.put("age","18");
        List list = Lists.newArrayList();
        for (int i = 0; i < 100000; i++) {
            list.add(UUID.randomUUID().toString());
        }
        context.put("list",list);
        try {
            StringWriter sw = new StringWriter();
            Template template = cfg.getTemplate("test.ftl", "utf-8");
            long one = System.currentTimeMillis();
            System.out.println(one);
            template.process(context,sw);
            System.out.println(System.currentTimeMillis()-one);
            //System.out.println(sw);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>测试title>
head>

<body>
<div class="main">
    <h2>测试h2>
    <div>
    ${name},年龄${age}
        <#list list as uuid>
        <div>${uuid}div>
        #list>
    div>
div>
body>

html>

总结

1.针对维护性来说,fm比vm来得更可靠一些。
2.针对性能来说,fm比vm更稳定,更快,但实质上,两者还是在同一个数量级上。如果是对并发量特别高的还是采用fm吧。

你可能感兴趣的:(模版引擎)