Re: Speed of Random Numbers

2019-08-03 Thread Giovanni Di Maria via Digitalmars-d-learn

Thank you very much to Everybody!
Giovanni



Where to put custom ldc2.conf (Windows)

2019-08-03 Thread NonNull via Digitalmars-d-learn

Perhaps I posted this to the wrong forum.
Help needed.
https://forum.dlang.org/post/pdqfquklkhambfccg...@forum.dlang.org



Re: Speed of Random Numbers

2019-08-03 Thread matheus via Digitalmars-d-learn
On Saturday, 3 August 2019 at 16:35:34 UTC, Giovanni Di Maria 
wrote:

For me the "goodness of random" is NOT important.


If that's the case, you could roll your own RNG:

//DMD64 D Compiler 2.072.2
import std.stdio;
import std.datetime;
import std.array, std.random;

void main(){
ubyte x;
auto r = benchmark!(f1,f2)(10_000);
writeln(r[0]);
writeln(r[1]);
}

int f1(){
static s = 10; // Seed
s = (214013*s+2531011); // [1]
s = (s>>16)&0x7FFF;
auto y=(s&7)+1;
//writeln(y);
return y;
}

int f2(){
byte c;
c=uniform!ubyte() % 8 +1;
//writeln(c);
return c;
}


/*
[1] 
https://software.intel.com/en-us/articles/fast-random-number-generator-on-the-intel-pentiumr-4-processor/

*/

/*
OUTPUT:
TickDuration(65263)   <-f1
TickDuration(635167)  <-f2
*/

Matheus.


Re: Speed of Random Numbers

2019-08-03 Thread bauss via Digitalmars-d-learn
On Saturday, 3 August 2019 at 17:47:46 UTC, Giovanni Di Maria 
wrote:
On Saturday, 3 August 2019 at 17:44:44 UTC, lithium iodate 
wrote:
On Saturday, 3 August 2019 at 16:35:34 UTC, Giovanni Di Maria 
wrote:

[...]


First off you could try to use a faster RNG engine than the 
default. The easiest way is to define a variable containing it 
and passing it to the functions each time.


auto rng = Xorshift(1234);
randomNumber = uniform!uint(rng);

This basic change approximately halved the 5 seconds your 
original example needs on my computer.
Another simple approach that I have tried is simply hashing 
the iterator using a fast hash function.
With xxHash32 I got the time down to 0.25 seconds. I also 
tried xxHash64 and FNV1a but they were not faster in my quick 
test.




Thank you very much Lithium Iodate
Now i will try it.
I let know you.
Thank you
Giovanni


If it doesn't matter if it's predictable or not then you could 
easily make your own simple random generator with would give 
"random" results.


Of course in general it's not usable:

import std.stdio;

class Random
{
private:
uint _seed;
uint _interval;

T abs(T)(T x)
{
T y = x > 0 ? T.max : cast(T)0;

return (x ^ y) - y;
}

public:
this()
{
import core.stdc.time;

_seed = cast(uint)time(null);
_interval = (_seed - 20559);
}

T next(T)(T max)
{
auto value = cast(T)(abs(_interval) % T.max);

_interval -= (_interval / 10) * _seed;

return value;
}
}

void main()
{
auto random = new Random;

foreach (_; 0 .. 1000)
{
auto result = random.next!ubyte(255);
writeln(result);
}
}




Re: Speed of Random Numbers

2019-08-03 Thread Dennis via Digitalmars-d-learn
On Saturday, 3 August 2019 at 16:35:34 UTC, Giovanni Di Maria 
wrote:
Do you know other faster functions or methods to generate 
random numbers?


For me the "goodness of random" is NOT important.


I found some nice random functions in this public-domain C 
single-header library collection, one of which is GameRand:


https://github.com/mattiasgustavsson/libs/blob/022370a79cf2d5f87fb43b420834a069adb5fede/rnd.h#L449

Here's the D version:
```
struct GameRand
{
uint[2] state;
}

uint randomGameRand(ref GameRand gamerand) {
gamerand.state[0] = ( gamerand.state[0] << 16 ) + ( 
gamerand.state[0] >> 16 );

gamerand.state[0] += gamerand.state[1];
gamerand.state[1] += gamerand.state[0];
return gamerand.state[0];
}
```

It's really fast and decent enough for games (hence the name I 
suppose). See:

http://www.flipcode.com/archives/07-15-2002.shtml


Re: Speed of Random Numbers

2019-08-03 Thread Giovanni Di Maria via Digitalmars-d-learn

On Saturday, 3 August 2019 at 17:44:44 UTC, lithium iodate wrote:
On Saturday, 3 August 2019 at 16:35:34 UTC, Giovanni Di Maria 
wrote:

[...]


First off you could try to use a faster RNG engine than the 
default. The easiest way is to define a variable containing it 
and passing it to the functions each time.


auto rng = Xorshift(1234);
randomNumber = uniform!uint(rng);

This basic change approximately halved the 5 seconds your 
original example needs on my computer.
Another simple approach that I have tried is simply hashing the 
iterator using a fast hash function.
With xxHash32 I got the time down to 0.25 seconds. I also tried 
xxHash64 and FNV1a but they were not faster in my quick test.




Thank you very much Lithium Iodate
Now i will try it.
I let know you.
Thank you
Giovanni


Re: Speed of Random Numbers

2019-08-03 Thread lithium iodate via Digitalmars-d-learn
On Saturday, 3 August 2019 at 16:35:34 UTC, Giovanni Di Maria 
wrote:
Do you know other faster functions or methods to generate 
random numbers?


For me the "goodness of random" is NOT important.

Thank you very much
GIovanni Di Maria


First off you could try to use a faster RNG engine than the 
default. The easiest way is to define a variable containing it 
and passing it to the functions each time.


auto rng = Xorshift(1234);
randomNumber = uniform!uint(rng);

This basic change approximately halved the 5 seconds your 
original example needs on my computer.
Another simple approach that I have tried is simply hashing the 
iterator using a fast hash function.
With xxHash32 I got the time down to 0.25 seconds. I also tried 
xxHash64 and FNV1a but they were not faster in my quick test.


Re: Speed of Random Numbers

2019-08-03 Thread Giovanni Di Maria via Digitalmars-d-learn

On Saturday, 3 August 2019 at 17:17:23 UTC, Cym13 wrote:
On Saturday, 3 August 2019 at 16:35:34 UTC, Giovanni Di Maria 
wrote:

[...]


To what extent isn't the quality of randomness important to you?

Your posts reminds me of the way Doom (the original) did it for 
things like enemy behaviour and shot dispersion: they generated 
a static table of 256 random numbers once and any time they 
needed a random byte they just picked the next in the table. 
They didn't have any security or sciency concern and just 
wanted to provide a different game each time so that worked 
well for them. You won't find anything faster than that I think.





Exactly Cym13.
The important is to get a different number
Ok, thank you.
G


Re: Speed of Random Numbers

2019-08-03 Thread Cym13 via Digitalmars-d-learn
On Saturday, 3 August 2019 at 16:35:34 UTC, Giovanni Di Maria 
wrote:

Hi to everybody
I am doing some experiments about random numbers.
I need "extreme speed" for the generation for numbers from 1 to 
8.


Generating 500_000_000 numbers with this code:



-
import std.stdio, std.array, std.random;
void main()
{
byte c;
writeln("Start");
for(int k=1;k<=500_000_000;k++)
c=uniform!ubyte() % 8 +1;  //<<< === RANDOM
writeln("Stop");
}
-



I get these results:

c=uniform!ubyte() % 8 +1;  ==>>> Execution time: 15.563 s
c=cast(byte)uniform(1, 9); ==>>> Execution time: 24.218 s

Do you know other faster functions or methods to generate 
random numbers?


For me the "goodness of random" is NOT important.

Thank you very much
GIovanni Di Maria


To what extent isn't the quality of randomness important to you?

Your posts reminds me of the way Doom (the original) did it for 
things like enemy behaviour and shot dispersion: they generated a 
static table of 256 random numbers once and any time they needed 
a random byte they just picked the next in the table. They didn't 
have any security or sciency concern and just wanted to provide a 
different game each time so that worked well for them. You won't 
find anything faster than that I think.


Speed of Random Numbers

2019-08-03 Thread Giovanni Di Maria via Digitalmars-d-learn

Hi to everybody
I am doing some experiments about random numbers.
I need "extreme speed" for the generation for numbers from 1 to 8.

Generating 500_000_000 numbers with this code:



-
import std.stdio, std.array, std.random;
void main()
{
byte c;
writeln("Start");
for(int k=1;k<=500_000_000;k++)
c=uniform!ubyte() % 8 +1;  //<<< === RANDOM
writeln("Stop");
}
-



I get these results:

c=uniform!ubyte() % 8 +1;  ==>>> Execution time: 15.563 s
c=cast(byte)uniform(1, 9); ==>>> Execution time: 24.218 s

Do you know other faster functions or methods to generate random 
numbers?


For me the "goodness of random" is NOT important.

Thank you very much
GIovanni Di Maria






Re: How to get name of my application (project)

2019-08-03 Thread Jonathan M Davis via Digitalmars-d-learn
On Saturday, August 3, 2019 5:47:33 AM MDT Rémy Mouëza via Digitalmars-d-
learn wrote:
> On Saturday, 3 August 2019 at 09:26:03 UTC, Andrey wrote:
> > Hello, how to get name of my application (project) that we
> > write in dub.json? Is there any compile-time constant like
> > __MODULE__?
>
> If I understand the question correctly, you are looking for
> std.file.thisExePath:
> - http://dpldocs.info/experimental-docs/std.file.thisExePath.html
> - https://dlang.org/phobos/std_file.html#thisExePath

Also, the first element in the array passed to main is the name of the
executable.

- Jonathan M Davis






Re: 1 new

2019-08-03 Thread Bastiaan Veelo via Digitalmars-d-learn

On Friday, 2 August 2019 at 18:25:28 UTC, jmh530 wrote:
When I navigate to https://forum.dlang.org/ I have a message 
that says "1 new reply" to "your posts." Normally, I click on 
that "1 new reply" and find the post that's new, go to it, and 
the message disappears. However, it doesn't seem to go away 
anymore. I tried looking at many different old posts without 
luck. At one point it was up to "2 new replies," but I viewed 
that other post and it went back down to "1 new reply." Does 
anyone else have this?


I always have “2 new replies” on that page, no matter how often I 
take a look. The feature seems a bit broken to me.


Bastiaan.


Re: How to get name of my application (project)

2019-08-03 Thread James Blachly via Digitalmars-d-learn

On 8/3/19 5:26 AM, Andrey wrote:
Hello, how to get name of my application (project) that we write in 
dub.json? Is there any compile-time constant like __MODULE__?


Dear Andrey:

Perhaps this is similar to what you are looking for:

https://dlang.org/spec/grammar.html#SpecialKeyword

SpecialKeyword:
__FILE__
__FILE_FULL_PATH__
__MODULE__
__LINE__
__FUNCTION__
__PRETTY_FUNCTION__


These are available at compile time.  Kind regards.


Re: How to get name of my application (project)

2019-08-03 Thread drug via Digitalmars-d-learn

03.08.2019 12:26, Andrey пишет:
Hello, how to get name of my application (project) that we write in 
dub.json? Is there any compile-time constant like __MODULE__?


You can get it using $DUB_PACKAGE from Environment variables 
(https://dub.pm/package-format-sdl), for example using preBuildCommands 
you can generate a module and define the constant you need there.


Re: Can one customize unit tests?

2019-08-03 Thread Dennis via Digitalmars-d-learn
The out-of-the box unittest runner is pretty bare by design. It 
just runs unittest blocks in serial as functions where assert() 
failures are not undefined behavior. Assert messages are not very 
helpful, though the recently added flag `-checkaction=context` 
helps a lot.


Luckily there is a trait for getting unittests:
https://dlang.org/spec/traits.html#getUnitTests

So it is possible to make your own unittest runner, as people 
have done:

http://code.dlang.org/packages/silly
http://code.dlang.org/packages/unit-threaded
http://code.dlang.org/packages/dunit

On Saturday, 3 August 2019 at 13:31:02 UTC, PV wrote:
Is it possible to somehow customize syntax and running of unit 
tests?


You can add @("attributes") to your unittests.
The default test-runner will not do anything with them currently, 
but your own test runner can.


I'd looked into the documentation, found nothing, but perhaps I 
just missed it. If it is not possible, is it realistic to (very 
easily) hack the D compiler to implement such feature?


Shouldn't be necessary, I think what you want can be done with 
attributes and your own test runner.



Would such feature also work in betterC?


unittest blocks itself are supported in betterC, so you can 
probably make it work.





Can one customize unit tests?

2019-08-03 Thread PV via Digitalmars-d-learn
Is it possible to somehow customize syntax and running of unit 
tests?




How I could use it:

1. Running only the tests from recently modified source files.
2. Ability to add optional timeout constraint into the tests, and 
then check whether the test doesn't exceed it.


unittests (time < 20 ms) { ... }

3. Checking that tests do not leak memory (where applicable).
4. Invoking tests in both D code and in C, where similar 
framework exists.



I'd looked into the documentation, found nothing, but perhaps I 
just missed it. If it is not possible, is it realistic to (very 
easily) hack the D compiler to implement such feature?



Would such feature also work in betterC?




Re: How to get name of my application (project)

2019-08-03 Thread Bastiaan Veelo via Digitalmars-d-learn

On Saturday, 3 August 2019 at 09:26:03 UTC, Andrey wrote:
Hello, how to get name of my application (project) that we 
write in dub.json? Is there any compile-time constant like 
__MODULE__?


The name of an application is not a compile time constant: you 
can rename the executable at any time. Like Rémy said, 
thisExePath.baseName will get you the name at run time.


Bastiaan.


Re: Help me decide D or C

2019-08-03 Thread Russel Winder via Digitalmars-d-learn
On Fri, 2019-08-02 at 20:24 +, Jon Degenhardt via Digitalmars-d-learn
wrote:
[…]
> In my view, the most important thing is the decision you've 
> already made - to pick a programming language and learn it in a 
> reasonable bit of depth. Which programming language you choose is 
> less important. No matter which choice you make you'll have the 
> opportunity to learn skills that will transfer to other 
> programming languages.
> 
[…]

Knowing what a given programming language is best for is core here: so no
using Prolog to try and write an operating system on real hardware.

Knowing many paradigms well is proven experimentally (see the work by Petre,
Green, Gilmore, and others) to improve capability in any given language. So
knowing Java, Prolog, Lisp, Python, SQL, C, Go, Rust, D, Kotlin, Groovy, Ruby
to a goodly level of competence makes you a better programmer in any one of
them.

So no matter which language you learn always plan to learn others. In this
sense C and D are equal, but for applications I'd choose D over C.


-- 
Russel.
===
Dr Russel Winder  t: +44 20 7585 2200
41 Buckmaster Roadm: +44 7770 465 077
London SW11 1EN, UK   w: www.russel.org.uk



signature.asc
Description: This is a digitally signed message part


Re: [OT] Re: Using Haskell for teaching [was: Help me decide D or C]

2019-08-03 Thread Russel Winder via Digitalmars-d-learn
On Fri, 2019-08-02 at 23:24 -0600, Jonathan M Davis via Digitalmars-d-learn
wrote:
[…]
> The university I went to had an undergrad class on programming paradigms
> that I _think_ was required (maybe two even), but it was definitely just the
> focus of a small number of classes, whereas my experience is that you get a
> lot more out of it when you actually use a language with a different
> paradigm for a while rather than just doing one group of assignments in it -
> and when the class covers multiple programming paradigms, that also dilutes
> how much you get out of each. On some level, as with many things, a lot of
> it depends on how much the students decide to put into it on their own.
[…]

UCL used to have a 10 week module that tried to do three language to show that
different programming languages were best for different problems. Prolog,
Snobol, and ??? were the trio when we terminated the module as not being
effective. This reinforced having a third programming language module in the
compulsory programming sequence so as to have 10 weeks per language rather
than three. Still not really long enough but it worked a lot better.

I think universities will be far better able to teach programming now that
entrants already know Python (to a greater or lesser extent). The single
biggest problem will be (has always been) having academics on the staff
competent and willing to put on programming courses. Far too many academics in
universities are truly crap programmers and are the last people you want to
show their crapness to students.
 

-- 
Russel.
===
Dr Russel Winder  t: +44 20 7585 2200
41 Buckmaster Roadm: +44 7770 465 077
London SW11 1EN, UK   w: www.russel.org.uk



signature.asc
Description: This is a digitally signed message part


Re: Help me decide D or C

2019-08-03 Thread Russel Winder via Digitalmars-d-learn
On Fri, 2019-08-02 at 16:49 +, Alexandre via Digitalmars-d-learn wrote:
[…]
> 
> Do you thing D would be the right tool for the job at this point 
> for me? Assuming I have 2 goals in mind: 1) become a better 
> programmer and 2) want to make fun writing software for myself 
> and if possible show something I might be proud of.
> I thought C would be a better choice for the 1), because everyone 
> says it's great to see whats behind the hood and things like 
> that. My experience with C btw is CS50 course, plus around 
> 200/300 pages of some books, still reading and a few toy 
> projects. So, basically 0 knowledge. haha. But after reading your 
> opinion, I guess C might not be the right tool for me, since I 
> wont be doing any kind of thing related to hardware (I think).

Seeing what is behind the hood was important in the 1970s and 1980s because
the hardware was constrained and programming languages (other than perhaps
Lisp) were still trying to provide abstractions over the extant hardware. In
the 2000s and 2010s "seeing what is under the hood" is more or less irrelevant
for most applications programmers – this does not include people for whom hard
real time factors are critical to their software. Most application programmers
should be focusing on using the right algorithms and data structures for the
task and letting the compilers and their code generators worry about what is
"under the hood".

Programming for microcontrollers is a different game and C dominates there
still (I assume, it is over a decade since I was doing that stuff), and for
good reason, the algorithms and data structures are hardware oriented rather
than being abstract. And when your hardware has bit data structures, so does
your programming language: C compilers for 8051 and AVR chips have special
extensions to allow bit access.

As for your 2, what do you find fun to create software for? It is then a
question of choosing the language that best enables you to simply and easily
create the data structures and thence the algorithms for those problems. Being
able to show off software and be proud of it revolves around having the
simplest and most understandable expressions of the best data structures and
algorithms for the problem addressed.

C can be used for this but you end up hacking this very badly, and opening
yourself up to horrors such as buffer overrun. 

My pet project of the moment is a DVB player (Me TV). It was originally a C
program authored by someone else who then began transforming it to a mixed
C/C++ system. It's original goal was to compete with MythTV and Kodi, but it
lost. When I picked it up I decided to strip it all back so as to be a
lightweight player to complement rather than compete with MythTV and Kodi. It
became very clear, very quickly that C++11 and indeed C++14 were not the right
tools for the job (poor support for messaging between threads being the single
biggest problem). I decided to do a re-write from scratch in D using GtkD and
GStreamerD. It all went well, D provided all the tools for creating the right
abstractions; the right data structures and algorithms were easy to express
and the libraries gave really good access to the underlying C libraries.

But… the GStreamer folk had rejected D as an officially supported language for
plugins and official API wrapper/binding support. Does this matter given
Mike's efforts keeping GtkD and GStreamerD up to date? In my case yes. I had
to get involved in amendments to GStreamer itself and developing new API
wrapper. I guess I could have kept going with D as the implementation language
for Me TV, but it seemed right to switch to Rust, which is officially
supported by the GStreamer folk, so lots of extra support for GStreamer
changes and wrapper writing (I guess I will end up doing a D version for Mike
to add to GStreamerD). It turns out that Rust is actually the wrong tool for
the job of working with GObject systems and yet the GStreamer folk chose Rust
because they abhorred C++ and assumed D always came with a garbage collector
(which is true in this case) and they have a morbid hatred of garbage
collection (true for some bits of GStreamer which are hard real time). What
the gtk-rs people have done though is to provide an excellent (albeit not
totally complete as yet) Rust inheritance system for GObject-based code. Of
course D has inheritance and so no problem, D is better than Rust for this.
Objectively D is the better language for GObject-based software, and yet Rust
is in the driving seat.

Is this another "everyone used C for applications programming but shouldn't
have done" moment? Almost. Rust is fine for those cases where composition is
the right abstraction approach. D can also do this, ditto Go, and indeed Java,
Kotlin, etc. Where a core abstraction requirement is inheritance, as in
GObject-based systems, D beats Rust. And yet the gtk-rs folk have used Rust
anyway and created the abstractions.

What is the moral behind this story of mixed 

Re: How to get name of my application (project)

2019-08-03 Thread Rémy Mouëza via Digitalmars-d-learn

On Saturday, 3 August 2019 at 09:26:03 UTC, Andrey wrote:
Hello, how to get name of my application (project) that we 
write in dub.json? Is there any compile-time constant like 
__MODULE__?


If I understand the question correctly, you are looking for 
std.file.thisExePath:

- http://dpldocs.info/experimental-docs/std.file.thisExePath.html
- https://dlang.org/phobos/std_file.html#thisExePath



Re: Help me decide D or C

2019-08-03 Thread Russel Winder via Digitalmars-d-learn
On Fri, 2019-08-02 at 17:25 +, berni via Digitalmars-d-learn wrote:
> […]
> 
> Yes, that was intentional. What I wanted to say is, that I think, 
> that it would have been better, if C was never invented at all... 
> In that case, there would have been space for an other language 
> for writing operating systems, without that much bugs in its 
> design. (But one never knows afterwards...)

If C had not been invented by Ritchie, Thompson, et al., something very like
it would have been invented by someone else. BCPL and B were not really
gaining the traction they perhaps should have had, and Algol, Algol68,
FORTRAN, PL/1, COBOL, etc. were not really designed for writing operating
systems.

https://en.wikipedia.org/wiki/Timeline_of_programming_languages

The problem was not the invention of C, the problem was all those programmers
who stopped thinking about using the right tool for a given task in a given
context, and started using C for all programming situations. But it happened,
it is read-only history. Rust, D, and Go are the current ways out of the
tragedy that was using C for applications programming. C++ is finally catching
up.

-- 
Russel.
===
Dr Russel Winder  t: +44 20 7585 2200
41 Buckmaster Roadm: +44 7770 465 077
London SW11 1EN, UK   w: www.russel.org.uk



signature.asc
Description: This is a digitally signed message part


Re: Can I remove an element from a global associative array from within a class destructor?

2019-08-03 Thread JN via Digitalmars-d-learn

On Friday, 2 August 2019 at 23:13:10 UTC, realhet wrote:
Today I read the documentation about structs, unions and 
classes, but I haven't find any restrictions for the ~this() 
destructors.


Is there some extra rules regarding the GC and what I must not 
do in the destructors?


Class destructors are similar to Java's finalizers in behavior. D 
doesn't give you a guarantee when the class destructor is called 
and it doesn't give you a guarantee if it will be called at all, 
so it doesn't really work for RAII-like resource management. If 
you want RAII-like behavior, either use structs (struct 
destructors are deterministic), or wrap your class inside a 
scoped/refCounted wrapper from std.typecons.


How to get name of my application (project)

2019-08-03 Thread Andrey via Digitalmars-d-learn
Hello, how to get name of my application (project) that we write 
in dub.json? Is there any compile-time constant like __MODULE__?


Re: Can I remove an element from a global associative array from within a class destructor?

2019-08-03 Thread realhet via Digitalmars-d-learn
On Saturday, 3 August 2019 at 05:33:05 UTC, Jonathan M Davis 
wrote:
On Friday, August 2, 2019 5:13:10 PM MDT realhet via 
Digitalmars-d-learn wrote:

Hi,
...


Thank you!

Now I have 2 solutions in mind:
1. If I only want to track the count and totalBytes for a 
specific kind of reference, I will be able update those from the 
destructor.
2. If I wanna track all instances of resources, I will use 2 
classes instead of one class and an associative array:
- When I create a resource, I create class1 for the statistics, 
and store them in an assocArray. Then I create a class2 and 
inside it there is a reference to class1. Class2 will be given to 
the user. If the user don't use it anymore, the GC will destroy 
class2, and in it's destructor it will mark a field in class1. 
And it will be periodically checked, so the actual deletion of 
the GLBuffer and other statistics will be done by me.
With millions of resources it will be slow, but for a few 
thousand it's ok. (I can also put them in a hierarchy, but the 
most important is not to alloc/dealloc from ~this())


I hope it will be stable :D

Thank You again!