I have the following code that runs in about 8 seconds:
using Random
DEBUG = false
PRISONERS = 100
ATTEMPTS = 50
function tryit()
a = [1:1:PRISONERS;]
a = shuffle(a)
for i in 1:PRISONERS
if DEBUG
println("prisoner $i")
end
count = 0
check = i
while count <= ATTEMPTS
count += 1
if a[check] == i
if DEBUG
println("Attempt $count: Checking box $check and found my number")
end
break
else
if DEBUG
println("Attempt $count: Checking box $check and found $(a[check])")
end
check = a[check]
end
end
if count > ATTEMPTS
if DEBUG
println("Prisoner $i failed to find his number in 50 attempts")
end
return false
end
end
return true
end
function main()
tries = 100000
success = 0.0
for i in 1:tries
if tryit()
success += 1
end
end
println("Ratio of success = $(success / tries)")
end
main()
Moving the global variables inside the function cuts the runtime to a tenth:
using Random
function tryit()
DEBUG = false
PRISONERS = 100
ATTEMPTS = 50
a = [1:1:PRISONERS;]
a = shuffle(a)
for i in 1:PRISONERS
if DEBUG
println("prisoner $i")
end
count = 0
check = i
while count <= ATTEMPTS
count += 1
if a[check] == i
if DEBUG
println("Attempt $count: Checking box $check and found my number")
end
break
else
if DEBUG
println("Attempt $count: Checking box $check and found $(a[check])")
end
check = a[check]
end
end
if count > ATTEMPTS
if DEBUG
println("Prisoner $i failed to find his number in 50 attempts")
end
return false
end
end
return true
end
function main()
tries = 100000
success = 0.0
for i in 1:tries
if tryit()
success += 1
end
end
println("Ratio of success = $(success / tries)")
end
main()
I could not reproduce the problem with a smaller code example. I am using Julia version 1.7.2 on Linux Mint.
Why globals? If you are concerned because you want to keep the variables' values global at function call for some reason, aliasing them in your function signature can fix the slowdown problem:
function tryit(PRISONERS = PRISONERS, ATTEMPTS = ATTEMPTS, DEBUG = DEBUG)
...
Declaring the global variables as constants solved the problem.
using Random
const DEBUG = false
const PRISONERS = 100
const ATTEMPTS = 50
function tryit()
a = [1:1:PRISONERS;]
a = shuffle(a)
for i in 1:PRISONERS
if DEBUG
println("prisoner $i")
end
count = 0
check = i
while count <= ATTEMPTS
count += 1
if a[check] == i
if DEBUG
println("Attempt $count: Checking box $check and found my number")
end
break
else
if DEBUG
println("Attempt $count: Checking box $check and found $(a[check])")
end
check = a[check]
end
end
if count > ATTEMPTS
if DEBUG
println("Prisoner $i failed to find his number in 50 attempts")
end
return false
end
end
return true
end
function main()
tries = 100000
success = 0.0
for i in 1:tries
if tryit()
success += 1
end
end
println("Ratio of success = $(success / tries)")
end
main()
See Why is there a performance difference between global and local variables in non-interactive Julia programs? for technical explanations.
Related
I want to ask you a question regarding Shell.Application COM Object. The programming language I work in is AutoIT. I would like to know, how can I open a folder in the exact same Window a previous folder has been opened.
For instance, if I open the C:\Folder in a Window and it has a subfolder, then how can I make the Script open that subfolder in the same window the "parent folder" has been opened, without creating a new window for that. The only way I can open a Folder is like this:
global $ShellApplication = ObjCreate("Shell.Application")
$ShellApplication.Explore("C:\Folder")
Sleep(100)
$ShellApplication.Explore("C:\Folder\Subfolder")
but this way is the wrong way. I hope someone has got any idea about how this could be achieved.
I don't know whether this is what you want, but you can automate the current window like this:
#include <Array.au3>
Opt("SendKeyDelay", 1) ;5 milliseconds
Opt("SendKeyDownDelay", 1) ;1 millisecond
$pid = ShellExecute("explorer.exe ", #DesktopDir)
Sleep(300)
;~ $aWinList=WinList("[REGEXPCLASS:(Explore|Cabinet)WClass]")
;~ _ArrayDisplay($aWinList)
ConsoleWrite($pid & #CRLF)
$handle = _WinGetHandleByPID($pid)
Sleep(2000)
SendKeepActive($handle)
Send('{F4}')
Send('^a')
Send(#AppDataCommonDir & "{ENTER}")
Func _WinGetHandleByPID($vProcess, $nShow = 1)
; Get Window Handle by PID
; Author Hubertus
; derived from Smoke_N's script with the same name,
; but to handle more than one process with the same name
; and to return handle of newest window ($nShow = 2).
;
; $vProcess = Processname(e.g. "Notepad.exe") or Processnumber(e.g. 4711 )
; $nShow = -1 "All (Visble or not)", $nShow = 0 "Not Visible Only", $nShow = 1 "Visible Only", $nShow = 2 "return handle of newest window "
; #error = 0 Returns array of matches. Array[0][0] and #extended shows number of matches.
; #error = 0 and $nShow = 2 return handle of newest window
; Array[n][0] shows windows title. Array[n][1] shows windows handle. n = 1 to #extended
; #error = 1 Process not found.
; #error = 2 Window not found.
If Not ProcessExists($vProcess) Then Return SetError(1, 0, 0) ; no matching process
Local $iWinList, $aWinList = WinList()
Local $iResult, $aResult[UBound($aWinList)][2]
Local $iProcessList, $aProcessList = ProcessList($vProcess)
If $aProcessList[0][0] = 0 Then Local $aProcessList[2][2] = [[1, 0], ["", $vProcess]]
For $iWinList = 1 To $aWinList[0][0]
For $iProcessList = 1 To $aProcessList[0][0]
If WinGetProcess($aWinList[$iWinList][1]) = $aProcessList[$iProcessList][1] Then
If $nShow > -1 And Not $nShow = (2 = BitAND(WinGetState($aWinList[$iWinList][1]), 2)) Then ContinueLoop
$iResult += 1
$aResult[$iResult][0] = $aWinList[$iWinList][0]
$aResult[$iResult][1] = $aWinList[$iWinList][1]
EndIf
Next
Next
If $iResult = 0 Then Return SetError(2, 0, 0) ; no window found
ReDim $aResult[$iResult + 1][2]
$aResult[0][0] = $iResult
If $nShow = 2 Then Return SetError(0, $iResult, $aResult[1][1])
Return SetError(0, $iResult, $aResult)
EndFunc ;==>_WinGetHandleByPID
Is it possible to catch a SIGINT to stop a Julia program from running, but doing so in an "orderly" fashion?
function many_calc(number)
terminated_by_sigint = false
a = rand(number)
where_are_we = 0
for i in eachindex(a)
where_are_we = i
# do something slow...
sleep(1)
a[i] += rand()
end
a, where_are_we, terminated_by_sigint
end
many_calc(100)
Say I want to end it efter 30 seconds, because I didn't realize it would take so long, but don't want to throw away all the results, because I have another method to continue from where_are_we-1. Is it at all possible to stop it (softly) early, but using the SIGINT signal?
You can just use a try ... catch ... end and check if the error is an interrupt.
For your code:
function many_calc(number)
terminated_by_sigint = false
a = rand(number)
where_are_we = 0
try
for i in eachindex(a)
where_are_we = i
# do something slow...
sleep(1)
a[i] += rand()
end
catch my_exception
isa(my_exception, InterruptException) ? (return a, where_are_we, true) : error()
end
a, where_are_we, terminated_by_sigint
end
Will check if the exception is an interupt, and will return with the values if so. Else it will error.
I have a function, which is supposed to return zero, if the input cannot be converted to an integer.
But sometimes it fails, if the field from a resultset is not a proper value, whatever it is.
Function nulblank(str)
dim val
if IsNull(str) then
val=0
else
str = trim(str)
if isNumeric(str) then
val = cDbl(str)
else
val = 0
end if
end if
nulblank = val
end function
I get an error 0x80020009 on str = trim(str)
This function is only called on
set rs = conn.execute(sql)
i = nulblank(rs("somefield"))
How can I make this function "failsafe", so it never dies, but returns 0 on "bad" values?
I guess I could do on error resume next and if Err.Number <> 0 then something.
But what can be in a rs("somefield") which is not null, but cannot be trim()'ed?
That error usually relates to an empty recordset.
You should check that the recordset has a row before attempting to retrieve a column value, eg:
set rs = conn.execute(sql)
if not rs.eof then
i = nulblank(rs("somefield"))
end if
Case #1:
module try;
string inp = "my_var";
initial begin
$display("Here we go!");
case (inp)
"my_var" : $display("my_var");
default : $display("default");
endcase
end
endmodule
Output is my_var
Case #2
module try;
string inp = "my_var";
initial begin
$display("Here we go!");
case (inp)
"*var*" : $display("*var*");
default : $display("default");
endcase
end
endmodule
Output is default.
Is it possible to get a hit with wildcard search in a case statement?
SystemVerilog does not have any string regular expression matching methods built into the standard. The UVM has a package that has a uvm_re_match() function. You can import the UVM package to get access to this function even if you do not use any other UVM testbench features. Some simulators, such as ModelSim/Questa have these routines built in as an extension to SystemVerilog so that you can do
module try;
string inp = "my_var";
initial begin
$display("Here we go!");
case (1)
inp.match("*var*") : $display("*var*");
default : $display("default");
endcase
end
endmodule
I found a work-around :
function string match(string s1,s2);
int l1,l2;
l1 = s1.len();
l2 = s2.len();
match = 0 ;
if( l2 > l1 )
return 0;
for(int i = 0;i < l1 - l2 + 1; i ++)
if( s1.substr(i,i+l2 -1) == s2)
return s2;
endfunction
module try;
string target_id = "abc.def.ddr4_0";
string inp = "ddr4_0";
string processed_inp;
initial begin
$display("Here we go!");
for(int i=0;i<2;i++) begin
if (i == 1)begin
inp = "ddr4_1";
target_id = "abc.def.ddr4_1";
end
processed_inp = match(target_id, inp);
$display("input to case = %0s", processed_inp);
case (processed_inp)
"ddr4_0" : $display("ddr4_0 captured!");
"ddr4_1" : $display("ddr4_1 captured!");
default : $display("default");
endcase
end
end
endmodule
Output:
Here we go!
input to case = ddr4_0
ddr4_0 captured!
input to case = ddr4_1
ddr4_1 captured!
PS : Found this solution on the www.
Cannot find the link right now. Will post the reference soon.
I would like the score to increase only when this button is pressed. However when I tried running my code the score did not change... Any ideas?
thanks in advance!
here is my code:
score = 0
local scoreNumber = display.newText(score, 200, 30, nil, 20)
scoreNumber.xScale = 1.2
scoreNumber.yScale = 1.2
local scoreText = display.newText("score:", 150, 30, nil, 20)
scoreText.xScale = 1.2
scoreText.yScale = 1.2
local buttonPressed = false
local myButton = display.newRect(50,50,100,100)
local function scoretimer(event)
if buttonPressed then
score = score + 1
scoreNumber.text = score
end
end
local function buttonPressed(event)
if event.phase == "began" then
buttonPressed = true
elseif event.phase == "ended" then
buttonPressed = false
end
return true
end
myButton:addEventListener("touch", buttonPressed)
Runtime:addEventListener("enterFrame", scoretimer)
First you declare:
local buttonPressed = false
end then you declare it again as a function:
local function buttonPressed(event)
Rename your function and everything should go fine