java - Slow performance when wrapping BufferedReader in custom FilterReader -


while tinkering around i/o stuff, made interesting observation: custom filterreader seemed have unexpected performance overhead. try , diagnose issue, threw simple performance test:

import java.io.*;  abstract class test {     public final long timerun(reader in) throws ioexception     {         long start = system.nanotime();         run(in);         long end = system.nanotime();         return end - start;     }      protected abstract void run(reader in) throws ioexception; }  class wrapinfiltertest extends test {     private class letterfilterreader extends filterreader     {         public letterfilterreader(reader in)         {             super(in);         }          @override         public int read() throws ioexception         {             int read;             while ((read = in.read()) != -1)             {                 if (character.isletter(read))                     break;             }             return read;         }     }      @override     public void run(reader in) throws ioexception     {         try (reader letterreader = new letterfilterreader(in))         {             while (letterreader.read() != -1);         }     } }  class rawreadertest extends test {     @override     public void run(reader in) throws ioexception     {         while (readletter(in) != -1);     }      public int readletter(reader in) throws ioexception     {         int read;         while ((read = in.read()) != -1)         {             if (character.isletter(read))                 break;         }         return read;     } }  public class performancetest {     public static void main(string[] args) throws ioexception     {         string filepath = "/path/to/file.txt";          test[] tests = new test[] { new wrapinfiltertest(), new rawreadertest() };          (test test : tests)         {             reader r = new bufferedreader(new filereader(filepath));             system.out.println(test.timerun(r) + "ns");         }     } } 

in general, i've found custom filter approach can as 3x slower straight reading buffered reader. however, seems dependent on file content. example, if file contains strictly letters, i've found custom filter approach performs marginally faster! what's going on?


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -