# For \`x::Vector{Char}\`, which should I use \`string(x...)\` or \`join(x)\`?

**URL:** https://discourse.julialang.org/t/for-x-vector-char-which-should-i-use-string-x-or-join-x/115469
**Category:** Performance
**Tags:** question, performance, strings
**Created:** [June 11, 2024, 10:50am UTC](https://discourse.julialang.org/t/for-x-vector-char-which-should-i-use-string-x-or-join-x/115469 "2024-06-11T10:50:43Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![paalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paalon/32/5784_2.png) [@paalon](https://discourse.julialang.org/u/paalon)
#### Post date: [June 11, 2024, 10:50am UTC](https://discourse.julialang.org/t/for-x-vector-char-which-should-i-use-string-x-or-join-x/115469/1 "2024-06-11T10:50:43Z")

</div>

I have a black box variable `x::Vector{Char}` which satisfies `length(x) < 20` (actually, it is not a black box because it is open source, but I do not understand how it behaves yet because I have not read the implementation in detail). Furthermore, the program uses `string(x...)` to convert `x` to `String`. I think `join(x)` do the same thing. Which is better?

---

<div class="post-metadata">

### Author: ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)
#### Post date: [June 11, 2024, 10:54am UTC](https://discourse.julialang.org/t/for-x-vector-char-which-should-i-use-string-x-or-join-x/115469/2 "2024-06-11T10:54:58Z")

</div>

Just do `String(x)`. ~~That should also reuse the memory.~~ It does not for `Vector{Char}` but nonetheless is by far the fastest option.

---

<div class="post-metadata">

### Author: ![paalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paalon/32/5784_2.png) [@paalon](https://discourse.julialang.org/u/paalon)
#### Post date: [June 11, 2024, 11:03am UTC](https://discourse.julialang.org/t/for-x-vector-char-which-should-i-use-string-x-or-join-x/115469/3 "2024-06-11T11:03:36Z")

</div>

It’s approximately 6.67 times faster than `string(x...)`! Thanks!  
 ![スクリーンショット 2024-06-11 20.00.12](https://global.discourse-cdn.com/julialang/original/3X/c/d/cdb068be12d65ff9455aeb77a95588bd0824b51b.png)

---

<div class="post-metadata">

### Author: ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)
#### Post date: [June 11, 2024, 11:10am UTC](https://discourse.julialang.org/t/for-x-vector-char-which-should-i-use-string-x-or-join-x/115469/4 "2024-06-11T11:10:22Z")

</div>

Btw, these benchmarks are not quite accurate because x is untyped global and accessing these is slow. See the [Performance tips](https://docs.julialang.org/en/v1/manual/performance-tips/#Avoid-untyped-global-variables) (also a good read if you care about performance in Julia generally 😉 ). To avoid this in benchmark use interpolation with `$`.

I just put all these methods into functions and these are my results:

```julia-repl
julia> using BenchmarkTools
julia> f1(x) = String(x)
julia> f2(x) = string(x...)
julia> f3(x) = join(x)

julia> x = rand(Char, 20);
julia> @btime f1($x);
  76.042 ns (1 allocation: 104 bytes)
julia> @btime f2($x);
  882.564 ns (21 allocations: 424 bytes)
julia> @btime f3($x);
  846.847 ns (5 allocations: 416 bytes)

julia> x = rand(Char, 2000);
julia> @btime f1($x);
  7.979 μs (1 allocation: 7.81 KiB)
julia> @btime f2($x);
  69.075 μs (2002 allocations: 54.81 KiB)
julia> @btime f3($x);
  66.002 μs (9 allocations: 11.73 KiB)

```

So for me the difference is more like ~9 times faster and also less allocations 🙂

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [June 11, 2024, 2:18pm UTC](https://discourse.julialang.org/t/for-x-vector-char-which-should-i-use-string-x-or-join-x/115469/5 "2024-06-11T14:18:02Z")

</div>

> [@abraemer](#):
>
> `f2(x) = string(x...)`

What actually happens when you splat 2000 values? I feared everything would go crazy, but apparently not.

---

<div class="post-metadata">

### Author: ![evansenvy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evansenvy/32/209769_2.png) [@evansenvy](https://discourse.julialang.org/u/evansenvy)
#### Post date: [June 13, 2024, 6:21am UTC](https://discourse.julialang.org/t/for-x-vector-char-which-should-i-use-string-x-or-join-x/115469/6 "2024-06-13T06:21:47Z")

</div>

Hello,  
As per me for converting a small `Vector{Char}` (`x`) into a `String`, `string(x...)` is generally better due to its straightforward and efficient conversion without needing a separator. It’s ideal for cases where `length(x) < 20` and offers simplicity in implementation.  
I hope this will help you,  
Thank you

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [June 13, 2024, 7:01am UTC](https://discourse.julialang.org/t/for-x-vector-char-which-should-i-use-string-x-or-join-x/115469/7 "2024-06-13T07:01:25Z")

</div>

> [@evansenvy](#):
>
> As per me for converting a small `Vector{Char}` (`x`) into a `String`, `string(x...)` is generally better

How is this better than `String(x)`? It’s _less_ efficient, and _more_ complicated.
