Created
December 8, 2014 13:55
-
-
Save christoph-frick/070cb386d27176f6735a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import groovy.lang.BenchmarkInterceptor | |
class Bench { | |
static final String CSV = 'csv.csv' | |
void runOriginal() { | |
BigDecimal tot = 0G | |
new File(CSV).splitEachLine(";") { row -> | |
try { | |
tot += row[2].toBigDecimal() | |
} catch(Exception e) {} | |
} | |
assert 299998G==tot | |
} | |
@groovy.transform.CompileStatic | |
void runOriginalStatic() { | |
BigDecimal tot = 0G | |
new File(CSV).splitEachLine(";") { row -> | |
try { | |
tot += row[2].toBigDecimal() | |
} catch(Exception e) {} | |
} | |
assert 299998G==tot | |
} | |
void runLinesTokenized() { | |
BigDecimal tot = 0G | |
new File(CSV).eachLine() { row -> | |
try { | |
def st = new StringTokenizer(row, ";") | |
2.times{ st.nextToken() } | |
tot += st.nextToken().toBigDecimal() | |
} catch(Exception e) {} | |
} | |
assert 299998G==tot | |
} | |
@groovy.transform.CompileStatic | |
void runLinesTokenizedStatic() { | |
BigDecimal tot = 0G | |
new File(CSV).eachLine() { row -> | |
try { | |
def st = new StringTokenizer(row, ";") | |
2.times{ st.nextToken() } | |
tot += st.nextToken().toBigDecimal() | |
} catch(Exception e) {} | |
} | |
assert 299998G==tot | |
} | |
} | |
new File(Bench.CSV).withWriter{ w -> | |
w << "xxx\n" // missing index | |
w << "1;2;xxx\n" // no number | |
w << ((0..125)*.toString().join(";")+"\n")*149999 | |
} | |
def proxy = ProxyMetaClass.getInstance(Bench) | |
proxy.interceptor = new BenchmarkInterceptor() | |
proxy.use { | |
def bench = new Bench() | |
bench.runOriginal() | |
bench.runOriginalStatic() | |
bench.runLinesTokenized() | |
bench.runLinesTokenizedStatic() | |
} | |
proxy.interceptor.calls.findAll{ it.key.startsWith('run') }.each{ | |
println "$it.key: ${it.value[1]-it.value[0]}ms" | |
} | |
// runOriginal: 1209ms | |
// runOriginalStatic: 1684ms | |
// runLinesTokenized: 273ms | |
// runLinesTokenizedStatic: 160ms |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment