Comments (4)
solution.
In a generic method, branch when T is a string.
Change the sb.Append(values[i]);
to the following.
if (typeof(T) == typeof(string))
{
sb.Append(Unsafe.As<string>(values[i]));
}
else
{
sb.Append(values[i]);
}
The benchmark results are detailed below.
Method | Mean | Error | Gen 0 | Gen 1 | Gen 2 | Allocated |
---|---|---|---|---|---|---|
JoinEmptyList | 21.415 ns | NA | 0.0048 | - | - | 40 B |
ZJoinEmptyList | 15.892 ns | NA | - | - | - | - |
JoinList1 | 27.790 ns | NA | 0.0048 | - | - | 40 B |
ZJoinList1 | 34.238 ns | NA | 0.0114 | - | - | 96 B |
JoinArray1 | 3.721 ns | NA | - | - | - | - |
ZJoinArray1 | 39.208 ns | NA | 0.0114 | - | - | 96 B |
JoinEnumerable1 | 23.032 ns | NA | 0.0048 | - | - | 40 B |
ZJoinEnumerable1 | 54.425 ns | NA | 0.0162 | - | - | 136 B |
JoinList2 | 91.815 ns | NA | 0.0248 | - | - | 208 B |
ZJoinList2 | 54.395 ns | NA | 0.0200 | - | - | 168 B |
JoinArray2 | 26.446 ns | NA | 0.0201 | - | - | 168 B |
ZJoinArray2 | 63.063 ns | NA | 0.0200 | - | - | 168 B |
JoinEnumerable2 | 75.099 ns | NA | 0.0248 | - | - | 208 B |
ZJoinEnumerable2 | 76.057 ns | NA | 0.0248 | - | - | 208 B |
JoinList10 | 514.121 ns | NA | 0.2842 | 0.0019 | - | 2384 B |
ZJoinList10 | 177.401 ns | NA | 0.0908 | 0.0002 | - | 760 B |
JoinArray10 | 106.759 ns | NA | 0.0908 | 0.0002 | - | 760 B |
ZJoinArray10 | 202.832 ns | NA | 0.0908 | 0.0002 | - | 760 B |
JoinEnumerable10 | 413.451 ns | NA | 0.2847 | 0.0024 | - | 2384 B |
ZJoinEnumerable10 | 218.206 ns | NA | 0.0956 | 0.0002 | - | 800 B |
from zstring.
I edited the first comment to change the amount of data and add an array case.
System.Join seems to be optimized for arrays.
from zstring.
In #18, it was always faster than in version 2.1.2 when using string as a type argument.
However, String.Join
is faster in some cases.
Ratio(ZString Ver 2.1.2, base is CLR) | Ratio(udaken/ZString@6b94c72, base is CLR)) | |
---|---|---|
Join(string, List<string>) with empty List |
56.36% | 23.08% |
Join(string, List<string>) with 1 element |
174.39% ⚠ | 23.35% |
Join(string, string[]) with 1 element |
1327.81% ⚠ | 754.88% |
Join(string, IEnumerable<string>) with 1 element |
352.87% | 226.50% |
Join(string, List<string>) with 2 elements |
104.49% | 59.39% |
Join(string, string[]) with 2 elements |
361.29% | 297.61% |
Join(string, IEnumerable<string>) with 2 elements |
165.40% | 101.47% |
Join(string, List<string>) with 10 elements |
75.99% | 34.03% |
Join(string, string[]) with 10 elements |
421.44% | 196.26% |
Join(string, IEnumerable<string>) with 10 elements |
113.52% | 49.99% |
⚠ indicates that allocation will occur.
The benchmark results for the udaken/ZString@6b94c72 are detailed below.
Method | Mean | Error | Gen 0 | Gen 1 | Gen 2 | Allocated |
---|---|---|---|---|---|---|
JoinEmptyList | 17.441 ns | NA | 0.0048 | - | - | 40 B |
ZJoinEmptyList | 4.136 ns | NA | - | - | - | - |
JoinList1 | 25.531 ns | NA | 0.0048 | - | - | 40 B |
ZJoinList1 | 6.585 ns | NA | - | - | - | - |
JoinArray1 | 3.665 ns | NA | - | - | - | - |
ZJoinArray1 | 27.358 ns | NA | - | - | - | - |
JoinEnumerable1 | 23.882 ns | NA | 0.0048 | - | - | 40 B |
ZJoinEnumerable1 | 56.520 ns | NA | 0.0162 | - | - | 136 B |
JoinList2 | 95.549 ns | NA | 0.0248 | - | - | 208 B |
ZJoinList2 | 61.030 ns | NA | 0.0200 | - | - | 168 B |
JoinArray2 | 26.032 ns | NA | 0.0201 | - | - | 168 B |
ZJoinArray2 | 75.796 ns | NA | 0.0200 | - | - | 168 B |
JoinEnumerable2 | 72.158 ns | NA | 0.0248 | - | - | 208 B |
ZJoinEnumerable2 | 82.503 ns | NA | 0.0248 | - | - | 208 B |
JoinList10 | 519.987 ns | NA | 0.2842 | 0.0019 | - | 2384 B |
ZJoinList10 | 193.093 ns | NA | 0.0908 | 0.0002 | - | 760 B |
JoinArray10 | 108.597 ns | NA | 0.0908 | 0.0002 | - | 760 B |
ZJoinArray10 | 184.746 ns | NA | 0.0908 | 0.0002 | - | 760 B |
JoinEnumerable10 | 426.553 ns | NA | 0.2847 | 0.0024 | - | 2384 B |
ZJoinEnumerable10 | 217.014 ns | NA | 0.0956 | 0.0002 | - | 800 B |
from zstring.
I've released 2.1.3
, thanks for your great contribution.
from zstring.
Related Issues (20)
- ZString not compatible with Unity 2023.2 HOT 1
- Unittest failed HOT 1
- Naming conflict with TextMeshPro and their Extensions
- Feature request: make `Length` writable HOT 2
- Are there any precautions when repeating the passing method? HOT 1
- Still have GC?? HOT 1
- Unity 2021.3.2f1 is getting over 1kb of gc collection from zstring HOT 3
- Should not promote TMP_Text.SetCharArray because it allocates too much HOT 1
- ZString.Join is looklike bad HOT 4
- Any planned support for a Format() that takes a param array? HOT 1
- Workaround for nested ZString.Format calls HOT 1
- Utf8JsonWriter HOT 1
- Joining HashSets does not work, see PR with test HOT 2
- Compilation errors HOT 1
- About .NET 7.0 Support
- Mac os 13.4 Test("{0,-20} {1,5:F}", "Adam", decimal 40); HOT 1
- .Append(string) does not work when ZString is created as a readonly class member HOT 1
- v2.5.1 compile error: BitOperations duplicate in System.Runtime HOT 11
- Huge amounts of GC when appending floats HOT 1
- How does this compare to MikePopoloski/StringFormatter? HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from zstring.