# Replace char vs string

**URL:** https://discourse.julialang.org/t/replace-char-vs-string/44083
**Category:** General Usage
**Created:** [August 1, 2020, 11:23am UTC](https://discourse.julialang.org/t/replace-char-vs-string/44083 "2020-08-01T11:23:36Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![anon37204545](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon37204545](https://discourse.julialang.org/u/anon37204545)
#### Post date: [August 1, 2020, 11:23am UTC](https://discourse.julialang.org/t/replace-char-vs-string/44083/1 "2020-08-01T11:23:36Z")

</div>

```julia
julia> @btime replace(" a "^1000, ' ' => "a")
  120.199 μs (7 allocations: 6.94 KiB)
"..."

julia> @btime replace(" a "^1000, " " => "a")
  193.500 μs (7 allocations: 6.94 KiB)
"..."

julia> @btime replace(" a "^1000, ' ' => 'a')
  142.000 μs (6 allocations: 6.91 KiB)
"..."

julia> @btime replace(" a "^1000, " " => 'a')
  206.600 μs (7 allocations: 6.94 KiB)
"..."

```

Is this behavior documented somewhere? Apparently, for the most performant `replace`, `old => new` pair should be `Char => String`.

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [August 1, 2020, 12:01pm UTC](https://discourse.julialang.org/t/replace-char-vs-string/44083/2 "2020-08-01T12:01:45Z")

</div>

I do not remember seeing this before. Very interesting. One could expect that `Char => Char` was the most performant. But at least the `Char => ...` are the better than `String => ...` what makes complete sense.

---

<div class="post-metadata">

### Author: ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)
#### Post date: [August 1, 2020, 12:15pm UTC](https://discourse.julialang.org/t/replace-char-vs-string/44083/3 "2020-08-01T12:15:47Z")

</div>

This makes sense, because strings in Julia are UTF-8 encoded, whereas chars are always ~~16~~ 32 bits, which means that a char can have different lengths when inserted into a string. If a char is to be replaced, this can be still efficient, because `replace` has to look ahead at most 4 bytes, whereas when a string is on the left hand side, it has to look ahead the entire length of that string, so this extra logic required probably causes replacement of strings to be a bit slower. When inserting a char into a string though, it first has to calculate how many characters that char occupies as UTF-8, whereas for a string this is just the byte length, which probably makes the replacement by a string a bit faster here. For more details on how exactly this is implemented, you would have to look at it’s source code and it’s not impossible, that there is still room for some microoptimization here.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [August 1, 2020, 12:54pm UTC](https://discourse.julialang.org/t/replace-char-vs-string/44083/4 "2020-08-01T12:54:05Z")

</div>

> [@simeonschaub](#):
>
> whereas chars are always 16 bits,

(`Char` is 32 bits.)

Yes, [the `write(io, ::Char)` method](https://github.com/JuliaLang/julia/blob/master/base/io.jl#L696-L704) could probably use some micro-optimization.
