digitalmars.D.learn - faster "stringification"
- Orut (21/21) Dec 10 2016 D nub here. I have a Python script that I'd like to implement in
- Stefan Koch (2/24) Dec 10 2016 Preallocate a static array for your result.
- Nicholas Wilson (7/29) Dec 10 2016 join performs allocations which is probably the reason for its
- Orut (4/11) Dec 11 2016 Thanks, Stefan and Nicholas. I think joiner did the trick (for
- Nicholas Wilson (2/15) Dec 11 2016 Excellent. that seem more like the numbers i would expect.
D nub here. I have a Python script that I'd like to implement in D. For certain parts, the D equivalent was slower than Python's. For example, Python code: #dummy code s = ["abc", "fjkd", "L", "qwa", "r", "uw", "tiro", "bc", "sg", "k", "jds", "yd"]; comparison D code: import std.stdio; import std.array; void main(string[] args){ string[] s = ["abc", "fjkd", "L", "qwa", "r", "uw", "tiro", "bc", "sg", "k", "jds", "yd"]; for(int i; i<10_000_000; i++) s.join("-"); //see Python comments } Python was 2x faster. How should I implement this in D?
Dec 10 2016
On Sunday, 11 December 2016 at 02:09:41 UTC, Orut wrote:D nub here. I have a Python script that I'd like to implement in D. For certain parts, the D equivalent was slower than Python's. For example, Python code: #dummy code s = ["abc", "fjkd", "L", "qwa", "r", "uw", "tiro", "bc", "sg", "k", "jds", "yd"]; conversions simplify comparison D code: import std.stdio; import std.array; void main(string[] args){ string[] s = ["abc", "fjkd", "L", "qwa", "r", "uw", "tiro", "bc", "sg", "k", "jds", "yd"]; for(int i; i<10_000_000; i++) s.join("-"); //see Python comments } Python was 2x faster. How should I implement this in D?Preallocate a static array for your result.
Dec 10 2016
On Sunday, 11 December 2016 at 02:09:41 UTC, Orut wrote:D nub here. I have a Python script that I'd like to implement in D. For certain parts, the D equivalent was slower than Python's. For example, Python code: #dummy code s = ["abc", "fjkd", "L", "qwa", "r", "uw", "tiro", "bc", "sg", "k", "jds", "yd"]; conversions simplify comparison D code: import std.stdio; import std.array; void main(string[] args){ string[] s = ["abc", "fjkd", "L", "qwa", "r", "uw", "tiro", "bc", "sg", "k", "jds", "yd"]; for(int i; i<10_000_000; i++) s.join("-"); //see Python comments } Python was 2x faster. How should I implement this in D?join performs allocations which is probably the reason for its slowness. There is joiner (in std.algorithm.iterations) that lazily performs the join, (though in the case of this "benchmark" will be cheating because you don't do anything with the result, print it to get a more fair comparison) avoiding allocation. see also appender (in std.array) for fast concatenation.
Dec 10 2016
On Sunday, 11 December 2016 at 02:46:58 UTC, Nicholas Wilson wrote:join performs allocations which is probably the reason for its slowness. There is joiner (in std.algorithm.iterations) that lazily performs the join, (though in the case of this "benchmark" will be cheating because you don't do anything with the result, print it to get a more fair comparison) avoiding allocation. see also appender (in std.array) for fast concatenation.Thanks, Stefan and Nicholas. I think joiner did the trick (for 50M iterations, ~2s for D, ~17s for Python).
Dec 11 2016
On Sunday, 11 December 2016 at 10:01:21 UTC, Orut wrote:On Sunday, 11 December 2016 at 02:46:58 UTC, Nicholas Wilson wrote:Excellent. that seem more like the numbers i would expect.join performs allocations which is probably the reason for its slowness. There is joiner (in std.algorithm.iterations) that lazily performs the join, (though in the case of this "benchmark" will be cheating because you don't do anything with the result, print it to get a more fair comparison) avoiding allocation. see also appender (in std.array) for fast concatenation.Thanks, Stefan and Nicholas. I think joiner did the trick (for 50M iterations, ~2s for D, ~17s for Python).
Dec 11 2016