Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Demo repository for habr.com article about faster Go string concatenation.

License

NotificationsYou must be signed in to change notification settings

quasilyte/concat

Repository files navigation

Demo package forУскорение конкатенации строк в Go своими руками article.

Overview

This package provides simple functions that return concatenation results.Can work faster than Go+ operator.

You should not use this package, really. It's just an example.

Benchmarks

BenchmarkConcat2Operator/short-8         20000000        84.4 ns/opBenchmarkConcat2Operator/longer-8        10000000       158 ns/opBenchmarkConcat2Builder/short-8          20000000        70.7 ns/opBenchmarkConcat2Builder/longer-8         10000000       127 ns/opBenchmarkConcat2/short-8                 30000000        57.3 ns/opBenchmarkConcat2/longer-8                20000000       106 ns/opBenchmarkConcat3Operator/short-8         20000000       103 ns/opBenchmarkConcat3Operator/longer-8        10000000       217 ns/opBenchmarkConcat3Builder/short-8          20000000        89.9 ns/opBenchmarkConcat3Builder/longer-8          5000000       249 ns/opBenchmarkConcat3/short-8                 20000000        85.0 ns/opBenchmarkConcat3/longer-8                10000000       189 ns/op

Number one is unsafe concatenation, second isstrings.Builder with preallocatedbuffer and "obvious" concatenation is the slowest one... unlessCL123256 is applied.

Using thebenchstat, here is the difference betweenconcat and+:

name              old time/op  new time/op  deltaConcat2/short-8   84.4ns ± 2%  64.3ns ± 4%  -23.85%  (p=0.000 n=14+15)Concat2/longer-8   138ns ± 1%   118ns ± 1%  -14.83%  (p=0.000 n=13+15)Concat3/short-8    105ns ± 5%    82ns ± 5%  -22.29%  (p=0.000 n=15+14)Concat3/longer-8   218ns ± 1%   192ns ± 1%  -11.95%  (p=0.000 n=15+15)

If compared with AMD64 asm version for concat2:

name              old time/op  new time/op  deltaConcat2/short-8   84.4ns ± 0%  56.9ns ± 5%  -32.54%  (p=0.000 n=15+15)Concat2/longer-8   138ns ± 1%   107ns ± 0%  -22.51%  (p=0.000 n=13+15)

As a bonus, asm version also makes empty strings concatenation optimization,just like runtime version of concat would.

Example

package mainimport ("fmt""github.com/Quasilyte/concat")funcmain() {v:="world!"fmt.Println(concat.Strings("hello, ",v))// => "hello, world!"}

About

Demo repository for habr.com article about faster Go string concatenation.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp