Go fmt Benchmark

A benchmark of different fmt functions, of the Go standard library.

Description:

The Go standard library has a lot of different functions to format strings. This benchmark compares the performance of some of them. This benchmark can be used to find the fastest function for a specific use-case.

Comparison

This section compares the performance of fmt implementations with the functions: println, printf and print

Different Run Count

Different CPU Core Count

Different Run Count Per Function

Some algorithms get slower or faster over time. Especially when they populate the same data struct.

println

printf

print

Different CPU Core Count Per Function

Some algorithms are influenced by the amount of cores a CPU has.

println

printf

print

Fmt

Function ns/op ops/sec B/op allocs/op MB/s
println 290.42 3460969 48.2 1 0.0
printf 301.48 3334621 48.2 1 0.0
print 296.41 3395945 48.2 1 0.0

Comparison

    const s = "Hello, World!"
    
    func BenchmarkFmt_println(b *testing.B) {
    	os.Stdout, _ = os.Open(os.DevNull)
    
    	for i := 0; i < b.N; i++ {
    		fmt.Println(s)
    	}
    }
    
    func BenchmarkFmt_print(b *testing.B) {
    	os.Stdout, _ = os.Open(os.DevNull)
    
    	for i := 0; i < b.N; i++ {
    		fmt.Print(s)
    	}
    }
    
    func BenchmarkFmt_printf(b *testing.B) {
    	os.Stdout, _ = os.Open(os.DevNull)
    
    	for i := 0; i < b.N; i++ {
    		fmt.Printf("%s\n", s)
    	}
    }

    Fmt println

    ns/op ops/sec B/op allocs/op MB/s
    290.42 3460969 48.2 1 0.0
    By Run Count
    By CPU Core Count

    Fmt printf

    ns/op ops/sec B/op allocs/op MB/s
    301.48 3334621 48.2 1 0.0
    By Run Count
    By CPU Core Count

    Fmt print

    ns/op ops/sec B/op allocs/op MB/s
    296.41 3395945 48.2 1 0.0
    By Run Count
    By CPU Core Count

    Full Benchmark Code

    const s = "Hello, World!"
    
    func BenchmarkFmt_println(b *testing.B) {
    	os.Stdout, _ = os.Open(os.DevNull)
    
    	for i := 0; i < b.N; i++ {
    		fmt.Println(s)
    	}
    }
    
    func BenchmarkFmt_print(b *testing.B) {
    	os.Stdout, _ = os.Open(os.DevNull)
    
    	for i := 0; i < b.N; i++ {
    		fmt.Print(s)
    	}
    }
    
    func BenchmarkFmt_printf(b *testing.B) {
    	os.Stdout, _ = os.Open(os.DevNull)
    
    	for i := 0; i < b.N; i++ {
    		fmt.Printf("%s\n", s)
    	}
    }

    Full Benchmark Output

    Implementation Function Runs CPU Core Count ns/op ops/sec B/op allocs/op MB/s
    Fmt println 1000 2 299.9 3334444.814938313 48 1 0
    Fmt println 2000 2 303.7 3292723.081988805 48 1 0
    Fmt println 3000 2 308.4 3242542.15304799 48 1 0
    Fmt println 4000 2 279.9 3572704.5373347625 48 1 0
    Fmt println 5000 2 280.5 3565062.3885918004 48 1 0
    Fmt println 6000 2 277.8 3599712.023038157 48 1 0
    Fmt println 7000 2 287.8 3474635.1633078526 48 1 0
    Fmt println 8000 2 285.8 3498950.314905528 48 1 0
    Fmt println 9000 2 277.7 3601008.2823190494 48 1 0
    Fmt println 10000 2 292.8 3415300.5464480873 48 1 0
    Fmt println 1000 8 360.1 2777006.38711469 49 1 0
    Fmt println 2000 8 283.9 3522367.0306445933 48 1 0
    Fmt println 3000 8 273.4 3657644.47695684 48 1 0
    Fmt println 4000 8 290.6 3441156.228492773 48 1 0
    Fmt println 5000 8 278.5 3590664.272890485 48 1 0
    Fmt println 6000 8 274.2 3646973.0123997084 48 1 0
    Fmt println 7000 8 270.9 3691399.04023625 48 1 0
    Fmt println 8000 8 273.4 3657644.47695684 48 1 0
    Fmt println 9000 8 297.6 3360215.053763441 48 1 0
    Fmt println 10000 8 280.6 3563791.8745545256 48 1 0
    Fmt println 1000 32 370.4 2699784.0172786177 52 1 0
    Fmt println 2000 32 324.3 3083564.6006783843 50 1 0
    Fmt println 3000 32 327 3058103.9755351683 49 1 0
    Fmt println 4000 32 283.4 3528581.5102328868 49 1 0
    Fmt println 5000 32 319.1 3133813.8514572233 48 1 0
    Fmt println 6000 32 271.1 3688675.765400221 48 1 0
    Fmt println 7000 32 279.4 3579098.067287044 48 1 0
    Fmt println 8000 32 266.1 3757985.719654265 48 1 0
    Fmt println 9000 32 270.2 3700962.250185048 48 1 0
    Fmt println 10000 32 291.8 3427004.797806717 48 1 0
    Fmt printf 1000 4 373.8 2675227.394328518 48 1 0
    Fmt printf 2000 4 330.6 3024803.387779794 48 1 0
    Fmt printf 3000 4 305.8 3270111.1837802483 48 1 0
    Fmt printf 4000 4 308.8 3238341.968911917 48 1 0
    Fmt printf 5000 4 319 3134796.238244514 48 1 0
    Fmt printf 6000 4 273.4 3657644.47695684 48 1 0
    Fmt printf 7000 4 280.1 3570153.5166012137 48 1 0
    Fmt printf 8000 4 278.6 3589375.448671931 48 1 0
    Fmt printf 9000 4 273.9 3650967.5063891932 48 1 0
    Fmt printf 10000 4 272.4 3671071.953010279 48 1 0
    Fmt printf 1000 8 313.6 3188775.5102040814 49 1 0
    Fmt printf 2000 8 301.1 3321155.7622052473 48 1 0
    Fmt printf 3000 8 313.9 3185727.9388340237 48 1 0
    Fmt printf 4000 8 326.2 3065603.923973023 48 1 0
    Fmt printf 5000 8 303.2 3298153.0343007916 48 1 0
    Fmt printf 6000 8 282.3 3542330.8537017354 48 1 0
    Fmt printf 7000 8 296 3378378.378378378 48 1 0
    Fmt printf 8000 8 277.7 3601008.2823190494 48 1 0
    Fmt printf 9000 8 272.9 3664345.914254306 48 1 0
    Fmt printf 10000 8 288.7 3463803.2559750606 48 1 0
    Fmt printf 1000 32 332.6 3006614.5520144315 52 1 0
    Fmt printf 2000 32 303.8 3291639.236339697 50 1 0
    Fmt printf 3000 32 318 3144654.0880503147 49 1 0
    Fmt printf 4000 32 301.7 3314550.8783559827 49 1 0
    Fmt printf 5000 32 287.9 3473428.2737061484 48 1 0
    Fmt printf 6000 32 278.8 3586800.573888092 48 1 0
    Fmt printf 7000 32 303.5 3294892.9159802306 48 1 0
    Fmt printf 8000 32 273.4 3657644.47695684 48 1 0
    Fmt printf 9000 32 300.6 3326679.97338656 48 1 0
    Fmt printf 10000 32 272.1 3675119.4413818447 48 1 0
    Fmt print 1000 1 338.9 2950722.9271171438 48 1 0
    Fmt print 2000 1 325.2 3075030.750307503 48 1 0
    Fmt print 3000 1 309.5 3231017.770597738 48 1 0
    Fmt print 4000 1 296.5 3372681.281618887 48 1 0
    Fmt print 5000 1 273.2 3660322.1083455347 48 1 0
    Fmt print 6000 1 289.8 3450655.6245686677 48 1 0
    Fmt print 7000 1 300.7 3325573.661456601 48 1 0
    Fmt print 8000 1 296 3378378.378378378 48 1 0
    Fmt print 9000 1 285.1 3507541.2136092596 48 1 0
    Fmt print 10000 1 300.8 3324468.085106383 48 1 0
    Fmt print 1000 2 283.7 3524850.193866761 48 1 0
    Fmt print 2000 2 303.3 3297065.611605671 48 1 0
    Fmt print 3000 2 282.8 3536067.892503536 48 1 0
    Fmt print 4000 2 288.4 3467406.3800277393 48 1 0
    Fmt print 5000 2 276.3 3619254.433586681 48 1 0
    Fmt print 6000 2 273.7 3653635.3671903545 48 1 0
    Fmt print 7000 2 285.8 3498950.314905528 48 1 0
    Fmt print 8000 2 276.5 3616636.528028933 48 1 0
    Fmt print 9000 2 274.2 3646973.0123997084 48 1 0
    Fmt print 10000 2 296.3 3374957.813027337 48 1 0
    Fmt print 1000 32 289.6 3453038.6740331487 52 1 0
    Fmt print 2000 32 296.3 3374957.813027337 50 1 0
    Fmt print 3000 32 303.1 3299241.174529858 49 1 0
    Fmt print 4000 32 318.1 3143665.513989311 49 1 0
    Fmt print 5000 32 272.9 3664345.914254306 48 1 0
    Fmt print 6000 32 266.5 3752345.21575985 48 1 0
    Fmt print 7000 32 301.8 3313452.6176275676 48 1 0
    Fmt print 8000 32 306.2 3265839.3207054213 48 1 0
    Fmt print 9000 32 275.7 3627130.9394269134 48 1 0
    Fmt print 10000 32 274.1 3648303.5388544323 48 1 0
    Fmt println 1000 4 318.5 3139717.425431711 48 1 0
    Fmt println 2000 4 279.4 3579098.067287044 48 1 0
    Fmt println 3000 4 295.4 3385240.352064997 48 1 0
    Fmt println 4000 4 282 3546099.290780142 48 1 0
    Fmt println 5000 4 278.9 3585514.521333812 48 1 0
    Fmt println 6000 4 281.6 3551136.3636363633 48 1 0
    Fmt println 7000 4 267.1 3743916.136278547 48 1 0
    Fmt println 8000 4 276.7 3614022.406938923 48 1 0
    Fmt println 9000 4 275.6 3628447.0246734396 48 1 0
    Fmt println 10000 4 276.8 3612716.76300578 48 1 0
    Fmt println 1000 16 370.4 2699784.0172786177 50 1 0
    Fmt println 2000 16 277.6 3602305.4755043224 49 1 0
    Fmt println 3000 16 280.4 3566333.808844508 48 1 0
    Fmt println 4000 16 312.9 3195909.2361776927 48 1 0
    Fmt println 5000 16 274.6 3641660.5972323376 48 1 0
    Fmt println 6000 16 272.8 3665689.149560117 48 1 0
    Fmt println 7000 16 294.7 3393281.3030200205 48 1 0
    Fmt println 8000 16 268.2 3728560.7755406415 48 1 0
    Fmt println 9000 16 289.1 3459010.7229332407 48 1 0
    Fmt println 10000 16 299.1 3343363.4236041456 48 1 0
    Fmt print 1000 4 285.6 3501400.5602240893 48 1 0
    Fmt print 2000 4 324 3086419.75308642 48 1 0
    Fmt print 3000 4 340.5 2936857.5624082233 48 1 0
    Fmt print 4000 4 284 3521126.76056338 48 1 0
    Fmt print 5000 4 283.5 3527336.860670194 48 1 0
    Fmt print 6000 4 269.3 3713330.857779428 48 1 0
    Fmt print 7000 4 310.1 3224766.204450177 48 1 0
    Fmt print 8000 4 275.1 3635041.802980734 48 1 0
    Fmt print 9000 4 263.9 3789314.134141721 48 1 0
    Fmt print 10000 4 346.2 2888503.755054882 48 1 0
    Fmt print 1000 8 291.2 3434065.9340659343 49 1 0
    Fmt print 2000 8 336 2976190.476190476 48 1 0
    Fmt print 3000 8 308.8 3238341.968911917 48 1 0
    Fmt print 4000 8 327.3 3055300.9471432935 48 1 0
    Fmt print 5000 8 295.8 3380662.6098715346 48 1 0
    Fmt print 6000 8 275.8 3625815.808556925 48 1 0
    Fmt print 7000 8 302.5 3305785.123966942 48 1 0
    Fmt print 8000 8 286.8 3486750.348675035 48 1 0
    Fmt print 9000 8 291.2 3434065.9340659343 48 1 0
    Fmt print 10000 8 293.2 3410641.2005457026 48 1 0
    Fmt print 1000 16 364.6 2742731.76083379 50 1 0
    Fmt print 2000 16 283.1 3532320.7347227125 49 1 0
    Fmt print 3000 16 407.5 2453987.73006135 48 1 0
    Fmt print 4000 16 296.2 3376097.23160027 48 1 0
    Fmt print 5000 16 272.3 3672420.124862284 48 1 0
    Fmt print 6000 16 281.4 3553660.270078181 48 1 0
    Fmt print 7000 16 274.4 3644314.868804665 48 1 0
    Fmt print 8000 16 297.1 3365870.0774150114 48 1 0
    Fmt print 9000 16 278.1 3595828.8385472847 48 1 0
    Fmt print 10000 16 278.3 3593244.6999640674 48 1 0
    Fmt printf 1000 2 307.7 3249918.752031199 48 1 0
    Fmt printf 2000 2 295.5 3384094.75465313 48 1 0
    Fmt printf 3000 2 298.5 3350083.752093802 48 1 0
    Fmt printf 4000 2 315 3174603.1746031744 48 1 0
    Fmt printf 5000 2 322.1 3104625.892579944 48 1 0
    Fmt printf 6000 2 272.1 3675119.4413818447 48 1 0
    Fmt printf 7000 2 308.2 3244646.3335496434 48 1 0
    Fmt printf 8000 2 303 3300330.0330033004 48 1 0
    Fmt printf 9000 2 278.5 3590664.272890485 48 1 0
    Fmt printf 10000 2 308 3246753.2467532465 48 1 0
    Fmt printf 1000 16 398.3 2510670.3489831784 50 1 0
    Fmt printf 2000 16 332.5 3007518.796992481 49 1 0
    Fmt printf 3000 16 291.4 3431708.9910775567 48 1 0
    Fmt printf 4000 16 308.9 3237293.622531564 48 1 0
    Fmt printf 5000 16 285.6 3501400.5602240893 48 1 0
    Fmt printf 6000 16 281.1 3557452.863749555 48 1 0
    Fmt printf 7000 16 302.7 3303600.9250082592 48 1 0
    Fmt printf 8000 16 322.5 3100775.19379845 48 1 0
    Fmt printf 9000 16 284.7 3512469.2658939236 48 1 0
    Fmt printf 10000 16 285.2 3506311.360448808 48 1 0
    Fmt println 1000 1 292.3 3421142.6616489906 48 1 0
    Fmt println 2000 1 285.9 3497726.477789437 48 1 0
    Fmt println 3000 1 288 3472222.222222222 48 1 0
    Fmt println 4000 1 293.9 3402517.863218782 48 1 0
    Fmt println 5000 1 285.9 3497726.477789437 48 1 0
    Fmt println 6000 1 287.9 3473428.2737061484 48 1 0
    Fmt println 7000 1 287.3 3480682.2137138876 48 1 0
    Fmt println 8000 1 277.6 3602305.4755043224 48 1 0
    Fmt println 9000 1 283.3 3529827.0384751144 48 1 0
    Fmt println 10000 1 291.1 3435245.6200618343 48 1 0
    Fmt printf 1000 1 312.4 3201024.3277848912 48 1 0
    Fmt printf 2000 1 300 3333333.3333333335 48 1 0
    Fmt printf 3000 1 321.2 3113325.0311332503 48 1 0
    Fmt printf 4000 1 300.6 3326679.97338656 48 1 0
    Fmt printf 5000 1 292.5 3418803.418803419 48 1 0
    Fmt printf 6000 1 290.8 3438789.54607978 48 1 0
    Fmt printf 7000 1 319.3 3131850.9238960226 48 1 0
    Fmt printf 8000 1 297.9 3356831.1513930853 48 1 0
    Fmt printf 9000 1 293.1 3411804.8447628794 48 1 0
    Fmt printf 10000 1 294.9 3390979.99321804 48 1 0