Re: How to gets multi results using tuple in D?

2021-12-23 Thread zjh via Digitalmars-d-learn

On Thursday, 23 December 2021 at 08:33:17 UTC, zoujiaqing wrote:


C++ Code:


also `...`,it's very useful.


Re: How to loop through characters of a string in D language?

2021-12-23 Thread Salih Dincer via Digitalmars-d-learn
On Thursday, 23 December 2021 at 16:13:49 UTC, Stanislav Blinov 
wrote:


You're comparing apples and oranges.
When benchmarking, at least look at
the generated assembly first.

I looked now and you're right. Insomuch that it should be 
eggplant not apple, banana not orange...:)


Because it's an irrelevant benchmarking!

Thank you all...


Re: How to loop through characters of a string in D language?

2021-12-23 Thread rumbu via Digitalmars-d-learn

On Thursday, 23 December 2021 at 07:14:35 UTC, Salih Dincer wrote:

It seems faster than algorithms in Phobos. We would love to see 
this in our new Phobos.



Replace: 436 msecs
Malloc : 259 msecs
*/


It seems because MallocReplace is cheating a lot:
- it is not called through another function like replace is 
called;

- accesses directly the constant str;
- assumes that it has a single character to replace;
- assumes that the character will be deleted not replaced with 
something;

- assumes that the character is always ';'
- assumes that the replacing string is not bigger than the 
replaced one, so it knows exactly how much space to allocate;
- does not have any parameter, at least on x86 this means that 
there is no arg pushes when it's called.
- does not return a string, just compares its result with another 
constant;


Since we already know all this stuff, we can go further :)

```d
string superFast()
{
enum r = str.replace(";", "");
return r;
}

```

Replace: 436 msecs
Malloc : 259 msecs
SuperFast: 0 msecs





Re: How to loop through characters of a string in D language?

2021-12-23 Thread Stanislav Blinov via Digitalmars-d-learn

On Thursday, 23 December 2021 at 07:14:35 UTC, Salih Dincer wrote:

It seems faster than algorithms in Phobos. We would love to see 
this in our new Phobos.


```d
  void mallocReplace()



  void normalReplace()
string result = str.replace(';',"");

}/* Console Out:
Replace: 436 msecs
Malloc : 259 msecs
*/
```


You're comparing apples and oranges. When benchmarking, at least 
look at the generated assembly first.


replace is not in Phobos, it's a D runtime vestige. It's not 
getting inlined even in release builds with lto, whereas that 
manual version would. Also, benchmark with runtime strings, not 
literals, otherwise the compiler might even swallow the thing 
whole.


What you're benchmarking is, basically, inlined optimized search 
in a literal versus a function call.


Re: How to gets multi results using tuple in D?

2021-12-23 Thread russhy via Digitalmars-d-learn
it's one of those things where D is starting to fall behind 
competition when it comes to quality of life features


while OP's example is not that bad, the AliasSeq is plain and 
simple just confusing.. it's a NO


also removing the need of import for tuple is needed!!


it's the same with sumtype/tagged unions and enums literals


we need a workgroup to tackle these issues quick

some new languages are building momentum, it'll  be hard to 
compete when they get released...


we better get ready now and maintain a good balance of new 
features without forgetting about quality of life


and deprecating/removing old features that holds us back


Re: How to gets multi results using tuple in D?

2021-12-23 Thread WebFreak001 via Digitalmars-d-learn

On Thursday, 23 December 2021 at 08:33:17 UTC, zoujiaqing wrote:


C++ Code:
```cpp
std::tuple DoIt()
{
return {false, 0, 0, "Hello"};
}

auto [r1, r2, r3, r4] = DoIt();

if (r1 == false)

```


D Code:

```D
Tuple!(bool, int, int, string) DoIt()
{
return [false, 1, 1, "Hello"];
}

auto result = DoIt();

auto r1= result[0];
auto r2= result[1];
auto r3= result[2];
auto r3= result[3];

if (r1 == false)
```

D requires more lines of code.


I think this is the best thing you can do:

```d
bool r1;
int r2, r3;
string r4;
AliasSeq!(r1, r2, r3, r4) = DoIt();
```

https://forum.dlang.org/thread/kmugmwmduxeoyfffo...@forum.dlang.org


How to gets multi results using tuple in D?

2021-12-23 Thread zoujiaqing via Digitalmars-d-learn



C++ Code:
```cpp
std::tuple DoIt()
{
return {false, 0, 0, "Hello"};
}

auto [r1, r2, r3, r4] = DoIt();

if (r1 == false)

```


D Code:

```D
Tuple!(bool, int, int, string) DoIt()
{
return [false, 1, 1, "Hello"];
}

auto result = DoIt();

auto r1= result[0];
auto r2= result[1];
auto r3= result[2];
auto r3= result[3];

if (r1 == false)
```

D requires more lines of code.