PowerShell v4, 144 bytes
$d=date;gjb|rjb
1..20|%{sajb{$x=date;sleep 3;((date)-$x).Ticks/1e7}>$null}
while(gjb -s "Running"){}(gjb|rcjb)-join'+'|iex
((date)-$d).Ticks/1e7
Sets $d
equal to Get-Date
, and clears out any existing job histories with Get-Job | Remove-Job
. We then loop 1..20|%{...}
and each iteration execute Start-Job
passing it the script block {$x=date;sleep 3;((date)-$x).ticks/1e7}
for the job (meaning each job will execute that script block). We pipe that output to >$null
in order to suppress the feedback (i.e., job name, status, etc.) that gets returned.
The script block sets $x
to Get-Date
, then Start-Sleep
for 3
seconds, then takes a new Get-Date
reading, subtracts $x
, gets the .Ticks
, and divides by 1e7
to get the seconds (with precision).
Back in the main thread, so long as any job is still -S
tatus "Running"
, we spin inside an empty while
loop. Once that's done, we Get-Job
to pull up objects for all the existing jobs, pipe those to Receive-Job
which will pull up the equivalent of STDOUT (i.e., what they output), -join
the results together with +
, and pipe it to iex
(Invoke-Expression
and similar to eval
). This will output the resultant sleep time plus overhead.
The final line is similar, in that it gets a new date, subtracts the original date stamp $d
, gets the .Ticks
, and divides by 1e7
to output the total execution time.
NB
OK, so this is a little bendy of the rules. Apparently on first execution, PowerShell needs to load a bunch of .NET assemblies from disk for the various thread operations as they're not loaded with the default shell profile. Subsequent executions, because the assemblies are already in memory, work fine. If you leave the shell window idle long enough, you'll get PowerShell's built-in garbage collection to come along and unload all those assemblies, causing the next execution to take a long time as it re-loads them. I'm not sure of a way around this.
You can see this in the execution times in the below runs. I started a fresh shell, navigated to my golfing directory, and executed the script. The first run was horrendous, but the second (executed immediately) worked fine. I then left the shell idle for a few minutes to let garbage collection come by, and then that run is again lengthy, but subsequent runs again work fine.
Example runs
Windows PowerShell
Copyright (C) 2014 Microsoft Corporation. All rights reserved.
PS H:\> c:
PS C:\> cd C:\Tools\Scripts\golfing
PS C:\Tools\Scripts\golfing> .\wait-a-minute.ps1
63.232359
67.8403415
PS C:\Tools\Scripts\golfing> .\wait-a-minute.ps1
61.0809705
8.8991164
PS C:\Tools\Scripts\golfing> .\wait-a-minute.ps1
62.5791712
67.3228933
PS C:\Tools\Scripts\golfing> .\wait-a-minute.ps1
61.1303589
8.5939405
PS C:\Tools\Scripts\golfing> .\wait-a-minute.ps1
61.3210352
8.6386886
PS C:\Tools\Scripts\golfing>
2I've read the question several times and I don't get it. Can you clarify a bit? Why "10 seconds" and a delay of "70 seconds"? How are all those times related? – Luis Mendo – 2016-06-16T07:31:02.050
@LuisMendo The whole program terminates in less than 10 seconds (about 5 in the example), but it had several parallel calls to a delay procedure. Each delay was 5 seconds long. 14 parallel delays of 5 seconds is 70 seconds. – Adám – 2016-06-16T07:33:32.930
@LuisMendo Better now? – Adám – 2016-06-16T07:42:59.747
3How many threads can we assume will be executed in parallel? – miles – 2016-06-16T07:47:08.303
@miles You chose how many you want, but it will have to be at least seven in order to finish on time. Or maybe I'm not understanding your question. – Adám – 2016-06-16T08:30:14.503
3What precision is required for the time in output? – edc65 – 2016-06-16T09:15:08.960
@edc65 Less than a second, so let us say 0.1 s. – Adám – 2016-06-16T09:15:59.537
Are you counting overhead in the "executed sleep time"? Your example seems to indicate so. Meaning, does our code have to explicitly have calculable sleep >60 (e.g., 10 threads at 6 seconds each), or just the actual runtime of the threads must be >60? – AdmBorkBork – 2016-06-16T13:13:43.357
@TimmyD The sleep itself must be ≥ 60. However, the sleep must actually be timed, and then it will be > 60 because of overhead (if nothing else, then in the timing procedure itself). – Adám – 2016-06-16T13:18:44.860
20I wonder if this will cause all the golfing language authors to engage in a mad dash to add multi-threading to their creations... – Adám – 2016-06-16T14:23:08.220
1I believe @miles wanted to know how many threads are the maximum. For threads >= 2/4/8 depending on the processor, the programs will not work. – NoOneIsHere – 2016-06-16T14:31:44.213
3@NoOneIsHere Ah, well, a properly implemented sleep method should not busy a core, so the number of threads may exceed the number of (virtual) processors. – Adám – 2016-06-16T15:08:44.737
This being code golf, do solutions have to be "golfed" (i.e. compressed to within an inch of their lives), or would you welcome an ungolfed solution in Golang (whose gofmt makes this tricky)? – Rob – 2016-06-16T15:09:07.480
@miles If you need real-time computed threads, then you may assume 8 – otherwise this isn't possible, I think. – Adám – 2016-06-16T15:09:21.773
@Rob In some languages the obvious solution is already (close to) the shortest. Besides, one way to view code-golf challenges is finding the shortest solution in EACH language. Otherwise Jelly will win most of the time... So: go ahead. – Adám – 2016-06-16T15:17:20.297
Done, thanks very much @Adám – Rob – 2016-06-16T15:50:50.997
Is the output format strictly
[a,b]
, or can it be any sort of delimited list / array, whatever is convenient for the language? – Digital Trauma – 2016-06-16T15:59:14.790@Rob to undo gofmt, try http://codebeautify.org/javaviewer
– cat – 2016-06-16T16:43:40.963Thanks @cat! @DigitalTrauma, yeah, that'd shave off some characters! Perhaps it's not too strict a rule? – Rob – 2016-06-16T16:47:17.497
@DigitalTrauma Adam, in a comment said "Any output format is good"; I wish this were in the question
– cat – 2016-06-16T16:55:03.813(why) do we need a new [tag:multi-threading] tag? – cat – 2016-06-16T17:54:10.393
1@cat Format phase added. Was there any such tag before? I think it is an interesting subject we hadn't had any challenges about. But I look forward to seeing more. By the way, my tag suggestion was peer reviewed and accepted. – Adám – 2016-06-16T18:12:17.747
I have a solution that works great if threads can burn CPU time instead of sleeping - is that OK, or is it absolutely required to call
sleep
? – nneonneo – 2016-06-17T08:05:46.937@nneonneo Feel free to post it, even if it isn't eligible to win. – Adám – 2016-06-17T08:13:53.070
1@Adám it originally was unclear, and I haven't retracted my close vote. But I think is it too broad now. – Rɪᴋᴇʀ – 2016-06-17T13:47:30.660
It's unclear what exactly counts as waiting multiple times. Do I have to fire a thread to not do anything, or is it OK to schedule a task? What if my language doesn't have threads, just an event loop (say, Javascript)? Is it okay then to set up a timer? – John Dvorak – 2016-06-17T17:11:38.067
2@JanDvorak The challenge says "wait" - it's pretty clear from that and the comments that the threads are supposed to sleep (as opposed to busy-waiting). – Mego – 2016-06-17T17:22:48.760
@JanDvorak Feel free to submit such a solution, even though it will not be eligible to win. – Adám – 2016-06-17T19:16:45.087
I'm asking about setting up timers. I wouldn't expect that to be legal, but you have blessed the Javascript javascript nevertheless. – John Dvorak – 2016-06-17T19:17:55.553
1Recommended reading: The Mythical Man-Month. – sergiol – 2017-02-19T00:46:17.233