Skip to content

Commit ee171d6

Browse files
reverse-string: add alternative span code
1 parent c2a5e8b commit ee171d6

File tree

1 file changed

+10
-0
lines changed
  • exercises/practice/reverse-string/.approaches/span

1 file changed

+10
-0
lines changed

exercises/practice/reverse-string/.approaches/span/content.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ So what is the limit for the amount of memory we can allocate?
5656
Well, this depends on how memory has already been allocated on the stack.
5757
That said, a small test program successfully stack-allocated memory for `750_000` characters, so you might be fine.
5858

59+
## Alternative
60+
61+
It is possible to use an alternative span-based implementation that is more readable, but has the downside of being about twice as slow:
62+
63+
```csharp
64+
Span<char> chars = stackalloc char[input.Length];
65+
input.AsSpan().CopyTo(chars);
66+
chars.Reverse();
67+
```
68+
5969
## Performance
6070

6171
If you're interested in how this approach's performance compares to other approaches, check the [performance approach][approach-performance].

0 commit comments

Comments
 (0)