Re: Epoch time + msecs

2015-11-14 Thread Handyman via Digitalmars-d-learn
On Friday, 13 November 2015 at 23:12:34 UTC, Steven Schveighoffer 
wrote:

If you cannot update to the latest compiler


Didn't realised this feature is so new to the compiler.  In my 
case, it is no problem to update the compiler, and I will.  
Thanks!


On Saturday, 14 November 2015 at 03:10:17 UTC, Jonathan M Davis 
wrote:

auto seed = Clock.currTime().stdTime;
(...) and you don't have to do any math.


Perfect.

On Saturday, 14 November 2015 at 03:10:17 UTC, Jonathan M Davis 
wrote:
Alternatively, std.random.unpredictable seed is there 
specifically to provide an unpredictable seed. It's probably 
best to just

use that if you don't have a good reason not to.


Of course.  That's why I mentioned my purpose of using 
Clock.currTime(), in the hope I got corrected in using the right 
and offical seed method which I failed to find, which brings me 
to another point.


The D docs seem very thorough and complete to me but less 
accessible in comparison to, e.g., Perl docs, Php docs, or Ruby 
docs.  In particular I have difficulties in understanding the 
headers of the standard library function specifications (e.g.,: 
auto uniform(string boundaries = "[)", T1, T2, 
UniformRandomNumberGenerator)(T1 a, T2 b, ref 
UniformRandomNumberGenerator urng) if 
(isFloatingPoint!(CommonType!(T1, T2)) && 
isUniformRNG!UniformRandomNumberGenerator);)


These headers are necessary and give loads of information. D is 
no toy language, that's for sure.  Maybe a tutorial should be 
written on how to read such headers and how to read the standard 
library docs in general.  I am making notes for myself, but these 
are of too little quality to be used in the public domain.


Like I said: I am spoiled by scripting languages.

Thanks again, your answers have helped me a lot.



Epoch time + msecs

2015-11-13 Thread Handyman via Digitalmars-d-learn
How to get current time as a float (or a double or a real) as a 
Unix epoch + milliseconds (e.g, 1447437383.465, or even 
1447437383.46512 with finer resolution)?   I read 
http://dlang.org/intro-to-datetime.html and the docs of course.  
I came this far


   auto ct = Clock.currTime();
   auto milliseconds = ct.fracSec.msecs;
   auto epoch = ct.toUnixTime();

But I think this is not the shortest way and I don't know how to 
combine and cast into a float (or a double or a real).


My goal was to multiply by 1000 and feed as this number as a seed 
to D's random number generator.  But there may be beter ways to 
make a seed.


Re: Epoch time + msecs

2015-11-13 Thread Handyman via Digitalmars-d-learn
On Friday, 13 November 2015 at 18:27:42 UTC, Steven Schveighoffer 
wrote:

On 11/13/15 1:00 PM, Handyman wrote:
What I would do is this:

auto t = (Clock.currTime() - 
SysTime.fromUnixTime(0)).total!"msecs" / 1000.0


Thanks, Steve.

   import std.stdio;
   import std.datetime;

   void main() {
  auto t = (Clock.currTime() - 
SysTime.fromUnixTime(0)).total!"msecs" / 1000.0;

  writeln(t);
   }

Gives an error: Error: no property 'fromUnixTime' for type 
'SysTime'.


When I do

   auto t = (Clock.currTime() - 
unixTimeToStdTime(0)).total!"msecs" / 1000.0;


I get
Error: incompatible types for ((currTime(opCall())) - 
(unixTimeToStdTime(0))): 'SysTime' and 'long'


and when I do

auto t = (Clock.currTime() - 
SysTime.fromUnixTime(0)).total!"msecs" / 1000.0;


I get:
Error: no property 'fromUnixTime' for type 'SysTime'

I think I am spoiled by 'typeless' scriping languages.


Re: parallel

2015-11-05 Thread Handyman via Digitalmars-d-learn

On Thursday, 5 November 2015 at 20:40:00 UTC, anonymous wrote:
So one of your four cores has to make two dishes. That takes 
two seconds.


So make fine-grained?

foreach (i; 0..50)
   Thread.sleep(20.msecs);

But then my program still says: '2 secs'.   Please enlighten me.



Re: parallel

2015-11-05 Thread Handyman via Digitalmars-d-learn

On Thursday, 5 November 2015 at 20:45:25 UTC, Ali Çehreli wrote:
That's still 1 second per task. The function prepare() cannot 
be executed by more than one core.


Thanks.  OK.  So 'prepare' is atomic?  Then let's turn it around: 
how can I make the cores prepare a meal of 5 dishes in 1.25 secs? 
  Should I rewrite, or split, 'prepare'?





Re: parallel

2015-11-05 Thread Handyman via Digitalmars-d-learn

On Thursday, 5 November 2015 at 20:54:37 UTC, anonymous wrote:
There is not attempt to split the `prepare` action up and run 
parts of it in parallel.


So 1.25 secs is impossible?


Re: parallel

2015-11-05 Thread Handyman via Digitalmars-d-learn

On Thursday, 5 November 2015 at 21:10:16 UTC, anonymous wrote:

parallel(iota(50)))


Wow. I have dealt with ranges and 'iota' (and with parallel), but 
I admit I have to think hard about this example.  Thanks a bunch 
all for your patience.




parallel

2015-11-05 Thread Handyman via Digitalmars-d-learn

import std.stdio;
import core.thread;
import std.datetime;  // for stopwatch
import std.parallelism;

void say(string s) {  // write and flush
   writeln(s);
   stdout.flush();
}

struct Dish {
   string name;
   void prepare() {
  say("Start with the " ~ name ~ ".");
  Thread.sleep(1.seconds); // kunstmatig tijd verbruiken
  say("Finished the " ~ name ~ ".");
   }
}

void main() {
   auto dishes = [ Dish("soup"), Dish("sauce"), Dish("fries"), 
Dish("fish"), Dish("ice") ];

   auto sw = StopWatch(AutoStart.yes);
   foreach (dish; parallel(dishes, 1)) dish.prepare();
   sw.stop;
   writefln("Diner is ready.  Cooking took %.3f seconds.", 
cast(float) sw.peek.msecs / 1000);

}

gives:

Start with the soup.
Start with the sauce.
Start with the fries.
Start with the fish.
Finished the sauce.
Finished the fries.
Start with the ice.
Finished the soup.
Finished the fish.
Finished the ice.
Diner is ready.  Cooking took 1.999 seconds.

Seems that 4 cores go all out on first 4 dishes, then 1 core 
deals with the last dish.  With 4 cores I expect diner is ready 
after 5/4 = 1.25 secs though.  What did I do wrong?


Enough introspection to report variable name of calling argument

2015-10-19 Thread Handyman via Digitalmars-d-learn
Is the following possible in D?  To call a (unary) function f 
with variable a, and let f print:



Called with argument named 'a'.


I am of course asking for a generic solution, independent of the 
actual variable name (in this case, 'a').  I am aware that I am 
likely asking the impossible because of how the function call 
mechanism is implemented in most programming languages, hence in 
D.  Still, it might be possible given D's stong introspection 
capabilities / traits / pragma's / whathaveyou's.