StringTokenizer - reading lines with integers

后端 未结 2 1956
温柔的废话
温柔的废话 2021-01-15 03:32

I have a question about optimization of my code (which works but is too slow...). I am reading an input in a form

X1 Y1
X2 Y2
etc

where Xi,

2条回答
  •  长发绾君心
    2021-01-15 04:11

    (updated answer)

    I can say that whatever the problems in your program speed, the choice of tokenizer is not one of them. After an initial run of each method to even out initialisation quirks, I can parse 1000000 rows of "12 34" in milliseconds. You could switch to using indexOf if you like but I really think you need to look at other bits of code for the bottleneck rather than this micro-optimisation. Split was a surprise for me - it's really, really slow compared to the other methods. I've added in Guava split test and it's faster than String.split but slightly slower than StringTokenizer.

    • Split: 371ms
    • IndexOf: 48ms
    • StringTokenizer: 92ms
    • Guava Splitter.split(): 108ms
    • CsvMapper build a csv doc and parse into POJOS: 237ms (or 175 if you build the lines into one doc!)

    The difference here is pretty negligible even over millions of rows.

    There's now a write up of this on my blog: http://demeranville.com/battle-of-the-tokenizers-delimited-text-parser-performance/

    Code I ran was:

    import java.util.StringTokenizer;
    import org.junit.Test;
    
    public class TestSplitter {
    
    private static final String line = "12 34";
    private static final int RUNS = 1000000;//000000;
    
    public final void testSplit() {
        long start = System.currentTimeMillis();
        for (int i=0;i

    eta: here's the guava code:

    public final void testGuavaSplit() {
        long start = System.currentTimeMillis();
        Splitter split = Splitter.on(" ");
        for (int i=0;i it = split.split(line).iterator();
            int x = Integer.parseInt(it.next());
            int y = Integer.parseInt(it.next());
        }
        System.out.println("GuavaSplit: "+(System.currentTimeMillis() - start)+"ms");
    }
    

    update

    I've added in a CsvMapper test too:

    public static class CSV{
        public int x;
        public int y;
    }
    
    public final void testJacksonSplit() throws JsonProcessingException, IOException {
        CsvMapper mapper = new CsvMapper();
        CsvSchema schema = CsvSchema.builder().addColumn("x", ColumnType.NUMBER).addColumn("y", ColumnType.NUMBER).setColumnSeparator(' ').build();
    
        long start = System.currentTimeMillis();
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < RUNS; i++) {
            builder.append(line);
            builder.append('\n');
        }       
        String input = builder.toString();
        MappingIterator it = mapper.reader(CSV.class).with(schema).readValues(input);
        while (it.hasNext()){
            CSV csv = it.next();
        }
        System.out.println("CsvMapperSplit: " + (System.currentTimeMillis() - start) + "ms");
    }
    

提交回复
热议问题