• Stars
    star
    300
  • Rank 138,870 (Top 3 %)
  • Language
    Rust
  • Created over 8 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

πŸ“ˆ benchmarking different ways to concatenate strings in rust

Comparing ways to concatenate strings in Rust 1.61 nightly (1.58 stable)

Intro

There are many ways to turn a &str into a String in Rust and therefore many ways to concatenate two &strs.

Here I benchmark several different ways to concatenate the strings "2014-11-28", "T" and "12:00:09Z" into "2014-11-28T12:00:09Z".

Thanks to all the comments on and discussion on reddit where I posted these originally only 7 benchmarks. Some go into the details of what is going on in the background of these operations.

How to run?

  • benchmarks: cargo +nightly bench
  • tests: cargo +nightly test --benches

Results (on my machine)

$ cargo +nightly bench

running 46 tests
test array_concat_test ... ignored
test array_join_long_test ... ignored
test array_join_test ... ignored
test collect_from_array_to_string_test ... ignored
test collect_from_vec_to_string_test ... ignored
test concat_in_place_macro_test ... ignored
test concat_string_macro_test ... ignored
test concat_strs_macro_test ... ignored
test format_macro_implicit_args_test ... ignored
test format_macro_test ... ignored
test from_bytes_test ... ignored
test joinery_test ... ignored
test mut_string_push_str_test ... ignored
test mut_string_push_string_test ... ignored
test mut_string_with_capacity_push_str_char_test ... ignored
test mut_string_with_capacity_push_str_test ... ignored
test mut_string_with_too_little_capacity_push_str_test ... ignored
test mut_string_with_too_much_capacity_push_str_test ... ignored
test string_concat_macro_test ... ignored
test string_from_all_test ... ignored
test string_from_plus_op_test ... ignored
test to_owned_plus_op_test ... ignored
test to_string_plus_op_test ... ignored
test array_concat                                 ... bench:          24 ns/iter (+/- 0)
test array_join                                   ... bench:          22 ns/iter (+/- 0)
test array_join_long                              ... bench:          24 ns/iter (+/- 0)
test collect_from_array_to_string                 ... bench:          30 ns/iter (+/- 0)
test collect_from_vec_to_string                   ... bench:          34 ns/iter (+/- 0)
test concat_in_place_macro                        ... bench:          14 ns/iter (+/- 0)
test concat_string_macro                          ... bench:          10 ns/iter (+/- 0)
test concat_strs_macro                            ... bench:          10 ns/iter (+/- 0)
test format_macro                                 ... bench:          52 ns/iter (+/- 0)
test format_macro_implicit_args                   ... bench:          53 ns/iter (+/- 0)
test from_bytes                                   ... bench:           0 ns/iter (+/- 0)
test joinery                                      ... bench:          46 ns/iter (+/- 0)
test mut_string_push_str                          ... bench:          24 ns/iter (+/- 0)
test mut_string_push_string                       ... bench:          68 ns/iter (+/- 1)
test mut_string_with_capacity_push_str            ... bench:          10 ns/iter (+/- 1)
test mut_string_with_capacity_push_str_char       ... bench:          10 ns/iter (+/- 0)
test mut_string_with_too_little_capacity_push_str ... bench:          39 ns/iter (+/- 0)
test mut_string_with_too_much_capacity_push_str   ... bench:          19 ns/iter (+/- 10)
test string_concat_macro                          ... bench:          10 ns/iter (+/- 0)
test string_from_all                              ... bench:          43 ns/iter (+/- 1)
test string_from_plus_op                          ... bench:          27 ns/iter (+/- 0)
test to_owned_plus_op                             ... bench:          29 ns/iter (+/- 0)
test to_string_plus_op                            ... bench:          27 ns/iter (+/- 0)

test result: ok. 0 passed; 0 failed; 23 ignored; 23 measured; 0 filtered out; finished in 33.39s

The same results rearranged fastest to slowest

0 ns/iter (+/- 0)         from_bytes
10 ns/iter (+/- 0)        concat_string_macro
10 ns/iter (+/- 0)        concat_strs_macro
10 ns/iter (+/- 0)        mut_string_with_capacity_push_str_char
10 ns/iter (+/- 0)        string_concat_macro
10 ns/iter (+/- 1)        mut_string_with_capacity_push_str
14 ns/iter (+/- 0)        concat_in_place_macro
19 ns/iter (+/- 10)       mut_string_with_too_much_capacity_push_str
22 ns/iter (+/- 0)        array_join
24 ns/iter (+/- 0)        array_concat
24 ns/iter (+/- 0)        array_join_long
24 ns/iter (+/- 0)        mut_string_push_str
27 ns/iter (+/- 0)        string_from_plus_op
27 ns/iter (+/- 0)        to_string_plus_op
29 ns/iter (+/- 0)        to_owned_plus_op
30 ns/iter (+/- 0)        collect_from_array_to_string
34 ns/iter (+/- 0)        collect_from_vec_to_string
39 ns/iter (+/- 0)        mut_string_with_too_little_capacity_push_str
43 ns/iter (+/- 1)        string_from_all
46 ns/iter (+/- 0)        joinery
52 ns/iter (+/- 0)        format_macro
53 ns/iter (+/- 0)        format_macro_implicit_args
68 ns/iter (+/- 1)        mut_string_push_string

Examples explained

array_concat()

let datetime = &[DATE, T, TIME].concat();

array_join()

let datetime = &[DATE, TIME].join(T);

array_join_long()

let datetime = &[DATE, T, TIME].join("");

collect_from_array_to_string()

let list = [DATE, T, TIME];
let datetime: String = list.iter().map(|x| *x).collect();

collect_from_vec_to_string()

let list = vec![DATE, T, TIME];
let datetime: String = list.iter().map(|x| *x).collect();

format_macro()

let datetime = &format!("{}{}{}", DATE, T, TIME);

format_macro_implicit_args()

let datetime = &format!("{DATE}{T}{TIME}");

from_bytes() ⚠️ don't actually do this

use std::ffi::OsStr;
use std::os::unix::ffi::OsStrExt;
use std::slice;

let bytes = unsafe { slice::from_raw_parts(DATE.as_ptr(), 20) };

let datetime = OsStr::from_bytes(bytes);

mut_string_push_str()

let mut datetime = String::new();
datetime.push_str(DATE);
datetime.push_str(T);
datetime.push_str(TIME);

mut_string_push_string()

let mut datetime = Vec::<String>::new();
datetime.push(String::from(DATE));
datetime.push(String::from(T));
datetime.push(String::from(TIME));
let datetime = datetime.join("");

mut_string_with_capacity_push_str()

let mut datetime = String::with_capacity(20);
datetime.push_str(DATE);
datetime.push_str(T);
datetime.push_str(TIME);

mut_string_with_capacity_push_str_char()

let mut datetime = String::with_capacity(20);
datetime.push_str(DATE);
datetime.push('T');
datetime.push_str(TIME);

mut_string_with_too_little_capacity_push_str()

let mut datetime = String::with_capacity(2);
datetime.push_str(DATE);
datetime.push_str(T);
datetime.push_str(TIME);

mut_string_with_too_much_capacity_push_str()

let mut datetime = String::with_capacity(200);
datetime.push_str(DATE);
datetime.push_str(T);
datetime.push_str(TIME);

string_from_all()

let datetime = &(String::from(DATE) + &String::from(T) + &String::from(TIME));

string_from_plus_op()

let datetime = &(String::from(DATE) + T + TIME);

to_owned_plus_op()

let datetime = &(DATE.to_owned() + T + TIME);

to_string_plus_op()

let datetime = &(DATE.to_string() + T + TIME);

Additional macro benches

concat_string_macro

#[macro_use(concat_string)]
extern crate concat_string;
let datetime = concat_string!(DATE, T, TIME);

concat_strs_macro

Unfortunately, this macro breaks RustAnalyzer.

#[macro_use(concat_strs)]
extern crate concat_strs;
let datetime = &concat_strs!(DATE, T, TIME);

string_concat_macro

#[macro_use]
extern crate string_concat;
let datetime = &string_concat::string_concat!(DATE, T, TIME);

concat_in_place_macro

let datetime = concat_in_place::strcat!(&mut url, DATE T TIME);

joinery

use joinery::prelude::*;
let vec = vec![DATE, T, TIME];
let datetime = &vec.iter().join_concat().to_string();

More Repositories

1

notify-rust

☝️send desktop notifications from your Rust app.
Rust
1,000
star
2

awesome-rust-testing

😎 Curated list of awesome things regarding Rust Testing
160
star
3

icalendar-rs

πŸ“† icalendar library, in Rust of course - for fun
Rust
125
star
4

toastify

🍞A commandline tool that shows desktop notifications using notify-rust
Rust
80
star
5

shell-uglify

Uglify Shellcode
Shell
21
star
6

wikitranslate-rs

πŸ“– Use Wikipedia as a dictionary
Rust
8
star
7

bill-rs

πŸ’Έ Tiny little billing library.
Rust
8
star
8

dateparser_benchmarks

πŸ“† comparing nom iso8601, chrono and rust-datetime
Rust
8
star
9

serde-json-schema

WIP json-schema implementation using serde
Rust
8
star
10

rosemary

⏳because we ain't got no thyme
Rust
8
star
11

battery-rs

πŸ”‹ tiny little indicator for your battery status (🐧 linux only)
Rust
6
star
12

fritz.rs

🐭 just me playing around with the fritz.box api
Rust
6
star
13

iterators.ts

πŸ›‹οΈ rustical iterators in typescript
TypeScript
6
star
14

sdp-nom

βš“οΈ i=a wasm friendly SDP parser
Rust
6
star
15

claude-rs

πŸ’Έ oppinionated fork of currency-rs
Rust
5
star
16

magnetoplasmadynamic-thruster

⏏️ Webinterface to MPD
Rust
5
star
17

guardgen

πŸ’‚πŸ»β€β™‚οΈ generate type guards from interface files
TypeScript
5
star
18

xxrb

πŸ€–extensible xmpp ruby bot
Ruby
4
star
19

rust-chess

little try at implementing chess in rust
Rust
3
star
20

dvb-rs

πŸš‹ talking to the dvb api
Rust
3
star
21

scrape

πŸͺš minimal scraping tool for personal use
Rust
3
star
22

webrtc-demo

Little Demonstrator for webrtc
Svelte
2
star
23

dotfiles

my very own dotfiles
Shell
2
star
24

parken-rs

just playing around with scrapers
Rust
2
star
25

signaler

Rust
2
star
26

oslab

my complex lab "operating systems"
C++
2
star
27

hannibal

a small actor library
Rust
2
star
28

old_dotfiles

my dotfiles
Vim Script
2
star
29

treeify-rs

🌳 tiny pretty printer for yaml
Rust
2
star
30

TIL

1
star
31

zutun

a very svelte todo list
HTML
1
star
32

sexy

❀️ the sexiest repo ever
HTML
1
star
33

vim-dojo

notes on a ~60min vim workshop
1
star
34

zummi

πŸ₯„ a criny tate for creating spunny foonerisms
Rust
1
star
35

cpp_feature_tests-rs

@erikzenker's Cpp-Feature-Tests in Rust
Rust
1
star
36

mini-cmb

mini catch my bus example
Rust
1
star
37

notification_osc_bridge

Services for streaming desktop notifications over osc, because!
Rust
1
star
38

hashmusic

little script that searches twitter for #hashmusic and adds the tweets content to mpd
Ruby
1
star
39

awesome-rust-actor-frameworks

πŸ•΄me coping with the amount of actor frameworks out there
1
star
40

dateberry

🌴 sweet little dates
HTML
1
star
41

ferris

lots of cute little crabs
1
star
42

talks

my talks
JavaScript
1
star
43

peerchat

Rust
1
star
44

rust-workshop-chat

πŸ‘¨β€πŸ«πŸ¦€ tiny beginners rust workshop
Rust
1
star
45

rss-cli

a tiny commandline tool to read news, finds rss or atom automatically in html
Ruby
1
star
46

emailaddress-rs

Email Address parsing library for Rust
Rust
1
star
47

sigur_ros

Rust
1
star
48

a-not-so-brief-intro-to-rust

πŸ‘¨β€πŸ«πŸ¦€ Extended version of "A very brief intro to rust" by rustbridge
1
star
49

cast-me

πŸ“½οΈ WS broker
Rust
1
star
50

gutsy

actix based websocket log receiver
Rust
1
star
51

DirCaster

turns any directory on your webserver into a podcast
1
star
52

Chumby-Radiolist-GUI

a tiny interface to modiy the channels on chumby
Python
1
star