Re: what exactly is string length?

2021-04-01 Thread mw via Digitalmars-d-learn
On Friday, 2 April 2021 at 05:18:49 UTC, H. S. Teoh wrote: On Fri, Apr 02, 2021 at 05:05:21AM +, mw via Digitalmars-d-learn wrote: [...] This is just an example, what if the exact length is not known statically, is there a functions to trim the `\0`s? What about `s.until('\0')`? Example:

Re: what exactly is string length?

2021-04-01 Thread Виталий Фадеев via Digitalmars-d-learn
On Friday, 2 April 2021 at 04:32:53 UTC, mw wrote: https://run.dlang.io/is/B4jcno --- import std; import std.conv : text; void main() { char[6] s; s = "abc"; writeln(s, s.length); // abc6, ok it's the static array's length string t = text("head-", s, "-tail");

Re: what exactly is string length?

2021-04-01 Thread rikki cattermole via Digitalmars-d-learn
On 02/04/2021 6:10 PM, Computermatronic wrote: On Friday, 2 April 2021 at 05:02:52 UTC, mw wrote: Ahh, I got what I see (from writeln) is not what get string here ;-) And I just tried: string t = text("head-", strip(s), "-tail"); It's the same behavior. So how can I trim the leading &

Re: what exactly is string length?

2021-04-01 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, Apr 02, 2021 at 05:05:21AM +, mw via Digitalmars-d-learn wrote: [...] > This is just an example, what if the exact length is not known > statically, is there a functions to trim the `\0`s? Another way, if you want to avoid the extra allocation, slice the static array with .indexOf:

Re: what exactly is string length?

2021-04-01 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, Apr 02, 2021 at 05:05:21AM +, mw via Digitalmars-d-learn wrote: [...] > This is just an example, what if the exact length is not known > statically, is there a functions to trim the `\0`s? What about `s.until('\0')`? Example: auto s = "abc\0\0\0def"; auto t = "blah"

Re: what exactly is string length?

2021-04-01 Thread Computermatronic via Digitalmars-d-learn
On Friday, 2 April 2021 at 05:02:52 UTC, mw wrote: Ahh, I got what I see (from writeln) is not what get string here ;-) And I just tried: string t = text("head-", strip(s), "-tail"); It's the same behavior. So how can I trim the leading & trailing `\0` from the static char array? strip

Re: what exactly is string length?

2021-04-01 Thread mw via Digitalmars-d-learn
On Friday, 2 April 2021 at 05:01:27 UTC, rikki cattermole wrote: On 02/04/2021 5:51 PM, mw wrote: Then how can I construct `t`? to make this assertion true:    assert(t == "head-abc-tail");  // failed! Slice it. string t = text("head-", s[0 .. 3], "-tail");

Re: what exactly is string length?

2021-04-01 Thread rikki cattermole via Digitalmars-d-learn
On 02/04/2021 5:51 PM, mw wrote: Then how can I construct `t`? to make this assertion true:    assert(t == "head-abc-tail");  // failed! Slice it. string t = text("head-", s[0 .. 3], "-tail"); http://ddili.org/ders/d.en/slices.html

Re: what exactly is string length?

2021-04-01 Thread mw via Digitalmars-d-learn
On Friday, 2 April 2021 at 04:54:07 UTC, Computermatronic wrote: On Friday, 2 April 2021 at 04:49:22 UTC, mw wrote: So you mean inside the writeln() call, the 0s are skipped? Well, if I use `string t` as filename, it will try to looking for a file called: "head-abc\0\0\0-tail" instead of

Re: what exactly is string length?

2021-04-01 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, Apr 02, 2021 at 04:32:53AM +, mw via Digitalmars-d-learn wrote: [...] > --- > import std; > import std.conv : text; > > > void main() > { > char[6] s; > s = "abc"; > writeln(s, s.length); // abc6, ok it's the static array's length > > string t = text("head-", s,

Re: what exactly is string length?

2021-04-01 Thread mw via Digitalmars-d-learn
On Friday, 2 April 2021 at 04:49:22 UTC, mw wrote: On Friday, 2 April 2021 at 04:43:48 UTC, rikki cattermole wrote: On 02/04/2021 5:38 PM, mw wrote: On Friday, 2 April 2021 at 04:36:01 UTC, rikki cattermole wrote: On 02/04/2021 5:32 PM, mw wrote: --- import std; import std.conv : text;

Re: what exactly is string length?

2021-04-01 Thread Computermatronic via Digitalmars-d-learn
On Friday, 2 April 2021 at 04:49:22 UTC, mw wrote: So you mean inside the writeln() call, the 0s are skipped? Well, if I use `string t` as filename, it will try to looking for a file called: "head-abc\0\0\0-tail" instead of just "head-abc-tail" ? or it's platform dependent? I would

Re: what exactly is string length?

2021-04-01 Thread mw via Digitalmars-d-learn
On Friday, 2 April 2021 at 04:43:48 UTC, rikki cattermole wrote: On 02/04/2021 5:38 PM, mw wrote: On Friday, 2 April 2021 at 04:36:01 UTC, rikki cattermole wrote: On 02/04/2021 5:32 PM, mw wrote: --- import std; import std.conv : text; void main() {     char[6] s;     s = "abc";    

Re: what exactly is string length?

2021-04-01 Thread mw via Digitalmars-d-learn
On Friday, 2 April 2021 at 04:38:37 UTC, mw wrote: On Friday, 2 April 2021 at 04:36:01 UTC, rikki cattermole wrote: I don't get it, what do you mean by the assertion: assert(t[9] == '\0'); t == "head-abc-tail" Just tried this: https://run.dlang.io/is/SFU5p4 ``` import std; import

Re: what exactly is string length?

2021-04-01 Thread rikki cattermole via Digitalmars-d-learn
On 02/04/2021 5:38 PM, mw wrote: On Friday, 2 April 2021 at 04:36:01 UTC, rikki cattermole wrote: On 02/04/2021 5:32 PM, mw wrote: --- import std; import std.conv : text; void main() {     char[6] s;     s = "abc";     writeln(s, s.length);  // abc6, ok it's the static array's length    

Re: what exactly is string length?

2021-04-01 Thread mw via Digitalmars-d-learn
On Friday, 2 April 2021 at 04:36:01 UTC, rikki cattermole wrote: On 02/04/2021 5:32 PM, mw wrote: --- import std; import std.conv : text; void main() {    char[6] s;    s = "abc";    writeln(s, s.length);  // abc6, ok it's the static array's length    string t = text("head-", s,

Re: what exactly is string length?

2021-04-01 Thread rikki cattermole via Digitalmars-d-learn
On 02/04/2021 5:32 PM, mw wrote: --- import std; import std.conv : text; void main() {    char[6] s;    s = "abc";    writeln(s, s.length);  // abc6, ok it's the static array's length    string t = text("head-", s, "-tail");    writeln(t, t.length);  // head-abc-tail16, why? assert(t[9]

what exactly is string length?

2021-04-01 Thread mw via Digitalmars-d-learn
https://run.dlang.io/is/B4jcno --- import std; import std.conv : text; void main() { char[6] s; s = "abc"; writeln(s, s.length); // abc6, ok it's the static array's length string t = text("head-", s, "-tail"); writeln(t, t.length); // head-abc-tail16, why? } --- Why

Re: Need for speed

2021-04-01 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, Apr 02, 2021 at 02:36:21AM +, Jon Degenhardt via Digitalmars-d-learn wrote: > On Thursday, 1 April 2021 at 19:55:05 UTC, H. S. Teoh wrote: [...] > > It's interesting that whenever a question about D's performance pops > > up in the forums, people tend to reach for optimization flags.

Re: Need for speed

2021-04-01 Thread Jon Degenhardt via Digitalmars-d-learn
On Thursday, 1 April 2021 at 19:55:05 UTC, H. S. Teoh wrote: On Thu, Apr 01, 2021 at 07:25:53PM +, matheus via Digitalmars-d-learn wrote: [...] Since this is a "Learn" part of the Foruam, be careful with "-boundscheck=off". I mean for this little snippet is OK, but for a other projects

Re: Best way to make a template function conditionally @trusted

2021-04-01 Thread tsbockman via Digitalmars-d-learn
On Friday, 2 April 2021 at 00:03:32 UTC, Paul Backus wrote: On Thursday, 1 April 2021 at 22:35:01 UTC, tsbockman wrote: Is there a better way? Here's a technique I've used: ... As long as the compile-time check is correct, this is sound: the @trusted lambda can be called from @safe code if

Re: Best way to make a template function conditionally @trusted

2021-04-01 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 1 April 2021 at 22:35:01 UTC, tsbockman wrote: Suppose I have a templated struct member function for which I can compute at compile-time when the function is memory safe, and when it is not. But, the compiler cannot correctly determine this automatically. What is the best way to

Best way to make a template function conditionally @trusted

2021-04-01 Thread tsbockman via Digitalmars-d-learn
Suppose I have a templated struct member function for which I can compute at compile-time when the function is memory safe, and when it is not. But, the compiler cannot correctly determine this automatically. What is the best way to express this in code? Other than straight-up duplicating

Re: Need for speed

2021-04-01 Thread H. S. Teoh via Digitalmars-d-learn
On Thu, Apr 01, 2021 at 09:16:09PM +, Imperatorn via Digitalmars-d-learn wrote: > On Thursday, 1 April 2021 at 21:13:18 UTC, H. S. Teoh wrote: [...] > > Thanks for the very interesting information; so it looks like most > > of the time spent is actually in copying array elements than > >

Re: Need for speed

2021-04-01 Thread Imperatorn via Digitalmars-d-learn
On Thursday, 1 April 2021 at 21:13:18 UTC, H. S. Teoh wrote: On Thu, Apr 01, 2021 at 01:17:15PM -0700, Ali Çehreli via Digitalmars-d-learn wrote: [...] [...] [...] Right, but in a typical program it's unpredictable whether there will be unused pages after the array. [...] [...]

Re: Need for speed

2021-04-01 Thread H. S. Teoh via Digitalmars-d-learn
On Thu, Apr 01, 2021 at 01:17:15PM -0700, Ali Çehreli via Digitalmars-d-learn wrote: > On 4/1/21 12:55 PM, H. S. Teoh wrote: > > > - Constructing large arrays by appending 1 element at a time with > > `~`. Obviously, this requires many array reallocations and the > > associated copying > > And

Re: Need for speed

2021-04-01 Thread Ali Çehreli via Digitalmars-d-learn
On 4/1/21 12:55 PM, H. S. Teoh wrote: > - Constructing large arrays by appending 1 element at a time with `~`. >Obviously, this requires many array reallocations and the associated >copying And that may not be a contributing factor. :) The following program sees just 15 allocations and

Re: Need for speed

2021-04-01 Thread ag0aep6g via Digitalmars-d-learn
On 01.04.21 21:53, Steven Schveighoffer wrote: Maybe, but I wasn't responding to that, just your statement not to recommend -boundscheck=off. In any case, it wouldn't hurt, right? Right.

Re: Need for speed

2021-04-01 Thread H. S. Teoh via Digitalmars-d-learn
On Thu, Apr 01, 2021 at 07:25:53PM +, matheus via Digitalmars-d-learn wrote: [...] > Since this is a "Learn" part of the Foruam, be careful with > "-boundscheck=off". > > I mean for this little snippet is OK, but for a other projects this my > be wrong, and as it says here: >

Re: Need for speed

2021-04-01 Thread Steven Schveighoffer via Digitalmars-d-learn
On 4/1/21 3:44 PM, ag0aep6g wrote: On 01.04.21 21:36, Steven Schveighoffer wrote: On 4/1/21 3:27 PM, ag0aep6g wrote: On 01.04.21 21:00, Berni44 wrote: ``` ldc2 -O3 -release -boundscheck=off -flto=full -defaultlib=phobos2-ldc-lto,druntime-ldc-lto speed.d ``` [...] Yes, but you can recommend

Re: Need for speed

2021-04-01 Thread ag0aep6g via Digitalmars-d-learn
On 01.04.21 21:36, Steven Schveighoffer wrote: On 4/1/21 3:27 PM, ag0aep6g wrote: On 01.04.21 21:00, Berni44 wrote: ``` ldc2 -O3 -release -boundscheck=off -flto=full -defaultlib=phobos2-ldc-lto,druntime-ldc-lto speed.d ``` [...] Yes, but you can recommend `-boundscheck=safeonly`, which

Re: Derived type

2021-04-01 Thread novice2 via Digitalmars-d-learn
thanks, i tried 2 variants: ```d struct Tnew {TBase payload; alias payload this;} ``` ```d enum Tnew : Tbase {init = Tbase.init} ``` both works, but 1-st not allow "2 level" cast: ```d struct Xptr {void* payload; alias payload this;} //Xptr based on void* struct Xobj {Xptr payload; alias

Re: Need for speed

2021-04-01 Thread Steven Schveighoffer via Digitalmars-d-learn
On 4/1/21 3:27 PM, ag0aep6g wrote: On 01.04.21 21:00, Berni44 wrote: ``` ldc2 -O3 -release -boundscheck=off -flto=full -defaultlib=phobos2-ldc-lto,druntime-ldc-lto speed.d ``` Please don't recommend `-boundscheck=off` to newbies. It's not just an optimization. It breaks @safe. If you want

Re: Need for speed

2021-04-01 Thread H. S. Teoh via Digitalmars-d-learn
On Thu, Apr 01, 2021 at 04:52:17PM +, Nestor via Digitalmars-d-learn wrote: [...] > ``` > import std.stdio; > import std.random; > import std.datetime.stopwatch : benchmark, StopWatch, AutoStart; > import std.algorithm; > > void main() > { > auto sw = StopWatch(AutoStart.no); >

Re: Need for speed

2021-04-01 Thread matheus via Digitalmars-d-learn
On Thursday, 1 April 2021 at 19:00:08 UTC, Berni44 wrote: Try using ldc2 instead of dmd: ``` ldc2 -O3 -release -boundscheck=off -flto=full -defaultlib=phobos2-ldc-lto,druntime-ldc-lto speed.d ``` should produce much better results. Since this is a "Learn" part of the Foruam, be careful

Re: Need for speed

2021-04-01 Thread ag0aep6g via Digitalmars-d-learn
On 01.04.21 21:00, Berni44 wrote: ``` ldc2 -O3 -release -boundscheck=off -flto=full -defaultlib=phobos2-ldc-lto,druntime-ldc-lto speed.d ``` Please don't recommend `-boundscheck=off` to newbies. It's not just an optimization. It breaks @safe. If you want to do welding without eye

Re: Need for speed

2021-04-01 Thread Imperatorn via Digitalmars-d-learn
On Thursday, 1 April 2021 at 19:00:08 UTC, Berni44 wrote: On Thursday, 1 April 2021 at 16:52:17 UTC, Nestor wrote: I was hoping to beat my dear Python and get similar results to Go, but that is not the case neither using rdmd nor running the executable generated by dmd. I am getting values

Re: Need for speed

2021-04-01 Thread Berni44 via Digitalmars-d-learn
On Thursday, 1 April 2021 at 16:52:17 UTC, Nestor wrote: I was hoping to beat my dear Python and get similar results to Go, but that is not the case neither using rdmd nor running the executable generated by dmd. I am getting values between 350-380 ms, and 81ms in Python. Try using ldc2

Re: Need for speed

2021-04-01 Thread Imperatorn via Digitalmars-d-learn
On Thursday, 1 April 2021 at 17:30:15 UTC, Chris Piker wrote: On Thursday, 1 April 2021 at 16:52:17 UTC, Nestor wrote: I was hoping to beat my dear Python and get similar results to Go, but that is not the case neither using rdmd nor running the executable generated by dmd. I am getting values

Re: Need for speed

2021-04-01 Thread Imperatorn via Digitalmars-d-learn
On Thursday, 1 April 2021 at 17:30:15 UTC, Chris Piker wrote: On Thursday, 1 April 2021 at 16:52:17 UTC, Nestor wrote: I was hoping to beat my dear Python and get similar results to Go, but that is not the case neither using rdmd nor running the executable generated by dmd. I am getting values

Re: Need for speed

2021-04-01 Thread Chris Piker via Digitalmars-d-learn
On Thursday, 1 April 2021 at 16:52:17 UTC, Nestor wrote: I was hoping to beat my dear Python and get similar results to Go, but that is not the case neither using rdmd nor running the executable generated by dmd. I am getting values between 350-380 ms, and 81ms in Python. Nice test. I'm new

Re: Need for speed

2021-04-01 Thread Imperatorn via Digitalmars-d-learn
On Thursday, 1 April 2021 at 16:52:17 UTC, Nestor wrote: I am a python programmer and I am enjoying Dlang and learning some programming insights on the way, thank everyone. I have no formal education and also program JS and PHP. Watching a video where a guy programs some simple code in

Re: Need for speed

2021-04-01 Thread Ali Çehreli via Digitalmars-d-learn
On 4/1/21 10:15 AM, ag0aep6g wrote: > Move `auto rnd = ...;` out of the loop, and you will get better times. Doing that reduces the time about 15 fold. Using Appender reduces it further a tiny bit: import std.array; // ... Appender!(int[]) mylist; // ... mylist.data.sort(); Ali

Re: Need for speed

2021-04-01 Thread ag0aep6g via Digitalmars-d-learn
On Thursday, 1 April 2021 at 16:52:17 UTC, Nestor wrote: I was hoping to beat my dear Python and get similar results to Go, but that is not the case neither using rdmd nor running the executable generated by dmd. I am getting values between 350-380 ms, and 81ms in Python. [...] ``` for

Re: Need for speed

2021-04-01 Thread Imperatorn via Digitalmars-d-learn
On Thursday, 1 April 2021 at 16:52:17 UTC, Nestor wrote: I am a python programmer and I am enjoying Dlang and learning some programming insights on the way, thank everyone. [...] Could you also post the python code for comparison?

Re: Need for speed

2021-04-01 Thread Imperatorn via Digitalmars-d-learn
On Thursday, 1 April 2021 at 17:16:06 UTC, Imperatorn wrote: On Thursday, 1 April 2021 at 16:52:17 UTC, Nestor wrote: I am a python programmer and I am enjoying Dlang and learning some programming insights on the way, thank everyone. [...] Could you also post the python code for comparison?

Need for speed

2021-04-01 Thread Nestor via Digitalmars-d-learn
I am a python programmer and I am enjoying Dlang and learning some programming insights on the way, thank everyone. I have no formal education and also program JS and PHP. Watching a video where a guy programs some simple code in Python and the same code in Go and compares speed I thought

Re: Asking about performance of std concatenation vs. Appender vs. custom class

2021-04-01 Thread Steven Schveighoffer via Digitalmars-d-learn
On 4/1/21 10:53 AM, ludo wrote: The results are below (you can also git clone the repo + dub test): | Concatenation method | benchmark in ms| |-|-| |with std:    | 385 ms| |with stdReserve: | 327 ms| |with stdLength:  | 29 ms| |with

Asking about performance of std concatenation vs. Appender vs. custom class

2021-04-01 Thread ludo via Digitalmars-d-learn
Hi fellow Dlangers, I am working on an old codebase, and found a re-implementation of standard dynamic arrays as a struct called [ArrayBuilder, visible on my github](https://tinyurl.com/2nr25ytt) The comment says: ```d /** * Behaves the same as built-in arrays, except about 6x faster with

Re: why use string for this example of appender?

2021-04-01 Thread ludo via Digitalmars-d-learn
Thank Steve, I open a new thread with some corrections, better title, etc. On Wednesday, 31 March 2021 at 22:05:12 UTC, Steven Schveighoffer wrote: On 3/31/21 5:32 PM, ludo wrote: [...] ArrayBuilder should be similar in performance to Appender. I think part of the issue with appender could

Re: Derived type

2021-04-01 Thread novice3 via Digitalmars-d-learn
On Thursday, 1 April 2021 at 12:07:17 UTC, WebFreak001 wrote: You can add a custom init value if you want to allow one: ```d enum Xobj : void* { init = null } ``` Thank you!

Re: Derived type

2021-04-01 Thread WebFreak001 via Digitalmars-d-learn
On Tuesday, 30 March 2021 at 19:02:09 UTC, novice2 wrote: [...] Strange syntax. Behavour exactly what i want, but this code not works for me :( enum Xobj : void*; Xobj var; //DMD Error: enum test7.Xobj forward reference of Xobj.init You can add a custom init value if you want to allow

Re: Static array initialisation

2021-04-01 Thread Ali Çehreli via Digitalmars-d-learn
On 4/1/21 2:30 AM, DLearner wrote: > immutable uint MemSize=100; // Memory size in bytes. > ubyte[MemSize] MemPool = 8; // Initialised to 8 for debugging. Valid index values there are from 0 to 99, inclusive. > WkPtr = [0]; > > counter = 1; > while (counter <= 102) { >

Re: Static array initialisation

2021-04-01 Thread user1234 via Digitalmars-d-learn
On Thursday, 1 April 2021 at 09:30:28 UTC, DLearner wrote: On Wednesday, 31 March 2021 at 23:21:59 UTC, russhy wrote: On Wednesday, 31 March 2021 at 17:54:38 UTC, DLearner wrote: [...] Can you show the print function? Maybe the problem lies there? Using rdmd, I believe the code below

Re: Static array initialisation

2021-04-01 Thread DLearner via Digitalmars-d-learn
On Wednesday, 31 March 2021 at 23:21:59 UTC, russhy wrote: On Wednesday, 31 March 2021 at 17:54:38 UTC, DLearner wrote: On Wednesday, 31 March 2021 at 17:46:25 UTC, Imperatorn wrote: On Wednesday, 31 March 2021 at 17:27:44 UTC, DLearner wrote: Hi I did: immutable uint MemSize=100; //

Re: Static array initialisation

2021-04-01 Thread Imperatorn via Digitalmars-d-learn
On Wednesday, 31 March 2021 at 17:27:44 UTC, DLearner wrote: Hi I did: immutable uint MemSize=100; // Memory size in bytes. // Memory Pool ubyte[MemSize] MemPool = 8; And had a look in memory. I think the compiler set up 101 '8's, not 100 in memory. Which I did not expect. Best