Julia語言的一些測試

lt發表於2017-03-19

1.語法老在變,第一個是0.3版文件中的例子,{}表示向量語法已經廢棄,第2個是0.6版文件的例子,在0.5版中是無效的,只有0.51自帶文件中的例子可以執行。

julia> for T = {Int8,Int16,Int32,Int64,Int128,Uint8,Uint16,Uint32,Uint64,Uint128}
       println("$(lpad(T,7)): [$(typemin(T)),$(typemax(T))]")
       end
ERROR: syntax: { } vector syntax is discontinued

julia> for T = [Int8,Int16,Int32,Int64,Int128,Uint8,Uint16,Uint32,Uint64,Uint128]
       println("$(lpad(T,7)): [$(typemin(T)),$(typemax(T))]")
       end
ERROR: UndefVarError: Uint8 not defined
 in anonymous at .\:?

julia> for T in [Int8,Int16,Int32,Int64,Int128,UInt8,UInt16,UInt32,UInt64,UInt128]
                println("$(lpad(T,7)): [$(typemin(T)),$(typemax(T))]")
              end
   Int8: [-128,127]
  Int16: [-32768,32767]
  Int32: [-2147483648,2147483647]
  Int64: [-9223372036854775808,9223372036854775807]
 Int128: [-170141183460469231731687303715884105728,170141183460469231731687303715884105727]
  UInt8: [0,255]
 UInt16: [0,65535]
 UInt32: [0,4294967295]
 UInt64: [0,18446744073709551615]
UInt128: [0,340282366920938463463374607431768211455]

julia>  x =Int128(2)^Int128(3)^Int128(4)
2417851639229258349412352

2.測試程式用時

julia> @elapsed [x for x in 1:10^6];

julia> @elapsed [x for x in 1:10^6]
0.151227431

julia> @elapsed [x for x in 1:10^7]
0.179493186

julia> @timed [x for x in 1:10^7]
([1,2,3,4,5,6,7,8,9,10  …  9999991,9999992,9999993,9999994,9999995,9999996,9999
997,9999998,9999999,10000000],0.147096148,40360520,0.098642467,Base.GC_Diff(4036
0520,1,0,12060,0,0,98642467,1,1))

julia> @time [x for x in 1:10^7]
  0.175094 seconds (12.06 k allocations: 38.491 MB, 57.12% gc time)
10000000-element Array{Int32,1}:
        1
        2
        3
        4
        5
        6
        7
        8
        9
       10
        ⋮
  9999991
  9999992
  9999993
  9999994
  9999995
  9999996
  9999997
  9999998
  9999999
 10000000

julia> tic(); [x for x in 1:10^7];toc()
elapsed time: 0.193216436 seconds
0.193216436

julia> tic(); [x for x in 1:10^7];toq()
0.070731702

3.二進位制位的顯示

julia>  x =Int128(2)^Int128(3)^Int128(4)
2417851639229258349412352

julia> bits(x)
"00000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000
000000000"

julia> bits(3.14)
"0100000000001001000111101011100001010001111010111000010100011111"

julia> bits(Float64(3.14))
"0100000000001001000111101011100001010001111010111000010100011111"

julia> bits(Float32(3.14))
"01000000010010001111010111000011"

julia> bits(Float16(3.14))
"0100001001001000"

相關文章