Skip to main content
added 188 characters in body
Source Link
h.j.k.
  • 19.4k
  • 3
  • 37
  • 93
StringBuilder script = new StringBuilder(
    "import java.io.IOException;\n" +
    "import java.io.Writer;\n" +
    "import java.util.Map;\n" +
    "public class C implements Template {\n" +
    "    public void write(Writer out, Map<String, String> params) throws IOException {\n"
);

Perhaps skip the String concatenation and use the append() methods too?

// 6401024 size is an arbitrary choice, 
// the code below + example template stands at under 400 chars already
// each substitution will use an additional 60 bytes or so... 
// multiply accordingly for 4 fields, 
// and finally setting aside more spaces for the actual replacements
StringBuilder script = new StringBuilder(6401024);
script.append("import java.io.IOException;")
    .append("import java.io.Writer;")
    .append("import java.util.Map;")
    .append("public class C implements Template{")
    .append("public void write(Writer out,Map<String, String> params) throws IOException{");

Also,

result.append('"');
// instead of
result.append("\"");

Have you actually tested the speed of this, using something like say jmh? I'll also be interested to know how this compares with something written and compiled with Nashorn (on Java 8)...

StringBuilder script = new StringBuilder(
    "import java.io.IOException;\n" +
    "import java.io.Writer;\n" +
    "import java.util.Map;\n" +
    "public class C implements Template {\n" +
    "    public void write(Writer out, Map<String, String> params) throws IOException {\n"
);

Perhaps skip the String concatenation and use the append() methods too?

// 640 size is an arbitrary choice, 
// the code below + example template stands at under 400 chars already
StringBuilder script = new StringBuilder(640);
script.append("import java.io.IOException;")
    .append("import java.io.Writer;")
    .append("import java.util.Map;")
    .append("public class C implements Template{")
    .append("public void write(Writer out,Map<String, String> params) throws IOException{");

Also,

result.append('"');
// instead of
result.append("\"");

Have you actually tested the speed of this, using something like say jmh? I'll also be interested to know how this compares with something written and compiled with Nashorn (on Java 8)...

StringBuilder script = new StringBuilder(
    "import java.io.IOException;\n" +
    "import java.io.Writer;\n" +
    "import java.util.Map;\n" +
    "public class C implements Template {\n" +
    "    public void write(Writer out, Map<String, String> params) throws IOException {\n"
);

Perhaps skip the String concatenation and use the append() methods too?

// 1024 size is an arbitrary choice, 
// the code below + example template stands at under 400 chars already
// each substitution will use an additional 60 bytes or so... 
// multiply accordingly for 4 fields, 
// and finally setting aside more spaces for the actual replacements
StringBuilder script = new StringBuilder(1024);
script.append("import java.io.IOException;")
    .append("import java.io.Writer;")
    .append("import java.util.Map;")
    .append("public class C implements Template{")
    .append("public void write(Writer out,Map<String, String> params) throws IOException{");

Also,

result.append('"');
// instead of
result.append("\"");

Have you actually tested the speed of this, using something like say jmh? I'll also be interested to know how this compares with something written and compiled with Nashorn (on Java 8)...

Source Link
h.j.k.
  • 19.4k
  • 3
  • 37
  • 93

StringBuilder script = new StringBuilder(
    "import java.io.IOException;\n" +
    "import java.io.Writer;\n" +
    "import java.util.Map;\n" +
    "public class C implements Template {\n" +
    "    public void write(Writer out, Map<String, String> params) throws IOException {\n"
);

Perhaps skip the String concatenation and use the append() methods too?

// 640 size is an arbitrary choice, 
// the code below + example template stands at under 400 chars already
StringBuilder script = new StringBuilder(640);
script.append("import java.io.IOException;")
    .append("import java.io.Writer;")
    .append("import java.util.Map;")
    .append("public class C implements Template{")
    .append("public void write(Writer out,Map<String, String> params) throws IOException{");

Also,

result.append('"');
// instead of
result.append("\"");

Have you actually tested the speed of this, using something like say jmh? I'll also be interested to know how this compares with something written and compiled with Nashorn (on Java 8)...