How to not print BenchmarkTools @time to cmd?

Viewed 29

I have the following Julia program:

using BenchmarkTools

function isPerm(x::Int, y::Int)
    
    numZeroX = 0
    numOneX = 0
    numTwoX = 0
    numThreeX = 0
    numFourX = 0
    numFiveX = 0
    numSixX = 0
    numSevenX = 0
    numEightX = 0
    numNineX = 0
    
    numZeroY = 0
    numOneY = 0
    numTwoY = 0
    numThreeY = 0
    numFourY = 0
    numFiveY = 0
    numSixY = 0
    numSevenY = 0
    numEightY = 0
    numNineY = 0
    
    strX = string(x,10,6)
    strY = string(y,10,6)
    
    for i in 1:length(strX)
        if cmp(strX[i:i],"0") > 0
            numZeroX = numZeroX + 1
        end
        if cmp(strX[i:i],"1") > 0
            numOneX = numOneX + 1
        end
        if cmp(strX[i:i],"2") > 0
            numTwoX = numTwoX + 1
        end
        if cmp(strX[i:i],"3") > 0
            numThreeX = numThreeX + 1
        end
        if cmp(strX[i:i],"4") > 0
            numFourX = numFourX + 1
        end
        if cmp(strX[i:i],"5") > 0
            numFiveX = numFiveX + 1
        end
        if cmp(strX[i:i],"6") > 0
            numSixX = numSixX + 1
        end
        if cmp(strX[i:i],"7") > 0
            numSevenX = numSevenX + 1
        end
        if cmp(strX[i:i],"8") > 0
            numEightX = numEightX + 1
        end
        if cmp(strX[i:i],"9") > 0
            numNineX = numNineX + 1
        end
    end
    
    for i in 1:length(strY)
        if cmp(strY[i:i],"0") > 0
            numZeroY = numZeroY + 1
        end
        if cmp(strY[i:i],"1") > 0
            numOneY = numOneY + 1 
        end
        if cmp(strY[i:i],"2") > 0
            numTwoY = numTwoY + 1
        end
        if cmp(strY[i:i],"3") > 0
            numThreeY = numThreeY + 1
        end
        if cmp(strY[i:i],"4") > 0
            numFourY = numFourY + 1
        end
        if cmp(strY[i:i],"5") > 0
            numFiveY = numFiveY + 1
        end
        if cmp(strY[i:i],"6") > 0
            numSixY = numSixY + 1
        end
        if cmp(strY[i:i],"7") > 0
            numSevenY = numSevenY + 1
        end
        if cmp(strY[i:i],"8") > 0
            numEightY = numEightY + 1
        end
        if cmp(strY[i:i],"9") > 0
            numNineY = numNineY + 1
        end
    end
    
    if numOneX == numOneY && numTwoX == numTwoY && numThreeX == numThreeY && numFourX == numFourY && numFiveX == numFiveY && numSixX == numSixY && numSevenX == numSevenY && numEightX == numEightY && numNineX == numNineY
        return 1
    end
    return 0
end

function twoXisPerm(p::Int)
    if isPerm(p,2*p) > 0
        return 1
    end
    return 0
end

function threeXisPerm(p::Int)
    if isPerm(p,3*p) > 0
        return 1
    end
    return 0
end

function fourXisPerm(p::Int)
    if isPerm(p,4*p) > 0
        return 1
    end
    return 0
end

function fiveXisPerm(p::Int)
    if isPerm(p,5*p) > 0
        return 1
    end
    return 0
end

function sixXisPerm(p::Int)
    if isPerm(p,6*p) > 0
        return 1
    end
    return 0
end

function main()
    x = 125874
    solution = 0
    while true
        if ((twoXisPerm(x) > 0) && (threeXisPerm(x) > 0) && (fourXisPerm(x) > 0) && (fiveXisPerm(x) > 0) && (sixXisPerm(x) > 0))
            global solution = x
            break
        end
        x = x + 1
    end
end

@time begin
main()
end

#run @time twice to ignore compilation overhead
@time begin 
main()
end
print("Solution: ", solution)
print("\n\nProgram completed in")

At the bottom of the above code, you can see I run @time twice to avoid the compilation overhead.

I am trying to get the output to look like the following:

Solution: 142857

Program completed in 89 milliseconds.

However, using the @time function or any of the other BenchmarkTools functions seems to always print the time directly to the terminal.

0.121307 seconds (3.42 M allocations: 110.138 MiB, 14.35% gc time, 10.43% compilation time)
0.089670 seconds (3.40 M allocations: 108.961 MiB, 5.16% gc time)

Is it possible to avoid automatically printing the execution time to the terminal and instead store to a variable?

1 Answers

Use @elapsed macro instead. For example:

julia> t = @elapsed rand(10^6);

julia> print("rand executed in $t seconds")
rand executed in 0.0016686 seconds
Related