|
| 1 | +println( "\nTest speed" ) |
| 2 | + |
| 3 | +const BENCH_REP = 200_000 |
| 4 | + |
| 5 | +function native_int() |
| 6 | + for i in 1:BENCH_REP |
| 7 | + @sprintf( "%10d", i ) |
| 8 | + end |
| 9 | +end |
| 10 | +function format_int() |
| 11 | + fmt = Printf.Format( "%10d" ) |
| 12 | + for i in 1:BENCH_REP |
| 13 | + Printf.format( fmt, i ) |
| 14 | + end |
| 15 | +end |
| 16 | +function runtime_int() |
| 17 | + for i in 1:BENCH_REP |
| 18 | + cfmt( "%10d", i ) |
| 19 | + end |
| 20 | +end |
| 21 | +function runtime_int_spec() |
| 22 | + fs = Format.FmtSpec("%10d") |
| 23 | + for i in 1:BENCH_REP |
| 24 | + cfmt( fs, i ) |
| 25 | + end |
| 26 | +end |
| 27 | +function runtime_int_bypass() |
| 28 | + f = generate_formatter( "%10d" ) |
| 29 | + for i in 1:BENCH_REP |
| 30 | + f( i ) |
| 31 | + end |
| 32 | +end |
| 33 | + |
| 34 | +println( "integer @sprintf speed") |
| 35 | +@time native_int() |
| 36 | +println( "integer format speed") |
| 37 | +@time format_int() |
| 38 | +println( "integer cfmt speed") |
| 39 | +@time runtime_int() |
| 40 | +println( "integer cfmt spec speed") |
| 41 | +@time runtime_int_spec() |
| 42 | +println( "integer cfmt speed, bypass repeated lookup") |
| 43 | +@time runtime_int_bypass() |
| 44 | + |
| 45 | +set_seed!(10) |
| 46 | +const testflts = [ _erfinv( rand() ) for i in 1:200_000 ] |
| 47 | + |
| 48 | +function native_float() |
| 49 | + for v in testflts |
| 50 | + @sprintf( "%10.4f", v) |
| 51 | + end |
| 52 | +end |
| 53 | +function format_float() |
| 54 | + fmt = Printf.Format( "%10.4f" ) |
| 55 | + for v in testflts |
| 56 | + Printf.format( fmt, v ) |
| 57 | + end |
| 58 | +end |
| 59 | +function runtime_float() |
| 60 | + for v in testflts |
| 61 | + cfmt( "%10.4f", v) |
| 62 | + end |
| 63 | +end |
| 64 | +function runtime_float_spec() |
| 65 | + fs = Format.FmtSpec("%10.4f") |
| 66 | + for v in testflts |
| 67 | + cfmt( fs, v ) |
| 68 | + end |
| 69 | +end |
| 70 | +function runtime_float_bypass() |
| 71 | + f = generate_formatter( "%10.4f" ) |
| 72 | + for v in testflts |
| 73 | + f( v ) |
| 74 | + end |
| 75 | +end |
| 76 | + |
| 77 | +println() |
| 78 | +println( "float64 @sprintf speed") |
| 79 | +@time native_float() |
| 80 | +println( "float64 format speed") |
| 81 | +@time format_float() |
| 82 | +println( "float64 cfmt speed") |
| 83 | +@time runtime_float() |
| 84 | +println( "float64 cfmt spec speed") |
| 85 | +@time runtime_float_spec() |
| 86 | +println( "float64 cfmt speed, bypass repeated lookup") |
| 87 | +@time runtime_float_bypass() |
0 commit comments