Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ScottPJones/2aa13df74e9c8432ef9b81cdc7db9f2e to your computer and use it in GitHub Desktop.
Save ScottPJones/2aa13df74e9c8432ef9b81cdc7db9f2e to your computer and use it in GitHub Desktop.
Comparison of @printf vs pr"..." output
julia> prx(nam,i,f,r) = pr"\%-5d(i), 0x\%x(i), \%(i), <\%-10s(nam)>, <\%10s(nam)>, \%7.3f(f), \%(r)\n"
prx (generic function with 1 method)
julia> @code_warntype prx("scott", 42, pi, 1//200)
Variables:
#self# <optimized out>
nam::String
i::Int64
f::Irrational{:π}
r::Rational{Int64}
Body:
begin
$(Expr(:inbounds, false))
# meta: location /Users/scott/.julia/v0.6/Format/src/cformat.jl cfmt 6
SSAValue(24) = $(Expr(:invoke, MethodInstance for generate_formatter(::String), :(Format.generate_formatter), "%-5d"))
SSAValue(23) = (Core._expr)(:call, SSAValue(24), i::Int64)::Expr
# meta: pop location
$(Expr(:inbounds, :pop))
SSAValue(26) = $(Expr(:foreigncall, :(:jl_toplevel_eval_in), Any, svec(Any, Any), :(Format.Format), 0, SSAValue(23), 0))
$(Expr(:inbounds, false))
# meta: location /Users/scott/.julia/v0.6/Format/src/cformat.jl cfmt 6
SSAValue(19) = $(Expr(:invoke, MethodInstance for generate_formatter(::String), :(Format.generate_formatter), "%x"))
SSAValue(18) = (Core._expr)(:call, SSAValue(19), i::Int64)::Expr
# meta: pop location
$(Expr(:inbounds, :pop))
SSAValue(21) = $(Expr(:foreigncall, :(:jl_toplevel_eval_in), Any, svec(Any, Any), :(Format.Format), 0, SSAValue(18), 0))
SSAValue(16) = $(Expr(:invoke, MethodInstance for #fmt#13(::Array{Any,1}, ::Function, ::Int64), :(Format.#fmt#13), :($(Expr(:foreigncall, :(:jl_alloc_array_1d), Array{Any,1}, svec(Any, Int64), Array{Any,1}, 0, 0, 0))), :(StringLiterals.fmt), :(i)))
$(Expr(:inbounds, false))
# meta: location /Users/scott/.julia/v0.6/Format/src/cformat.jl cfmt 6
SSAValue(12) = $(Expr(:invoke, MethodInstance for generate_formatter(::String), :(Format.generate_formatter), "%-10s"))
SSAValue(11) = (Core._expr)(:call, SSAValue(12), nam::String)::Expr
# meta: pop location
$(Expr(:inbounds, :pop))
SSAValue(14) = $(Expr(:foreigncall, :(:jl_toplevel_eval_in), Any, svec(Any, Any), :(Format.Format), 0, SSAValue(11), 0))
$(Expr(:inbounds, false))
# meta: location /Users/scott/.julia/v0.6/Format/src/cformat.jl cfmt 6
SSAValue(7) = $(Expr(:invoke, MethodInstance for generate_formatter(::String), :(Format.generate_formatter), "%10s"))
SSAValue(6) = (Core._expr)(:call, SSAValue(7), nam::String)::Expr
# meta: pop location
$(Expr(:inbounds, :pop))
SSAValue(9) = $(Expr(:foreigncall, :(:jl_toplevel_eval_in), Any, svec(Any, Any), :(Format.Format), 0, SSAValue(6), 0))
$(Expr(:inbounds, false))
# meta: location /Users/scott/.julia/v0.6/Format/src/cformat.jl cfmt 6
SSAValue(3) = $(Expr(:invoke, MethodInstance for generate_formatter(::String), :(Format.generate_formatter), "%7.3f"))
SSAValue(2) = (Core._expr)(:call, SSAValue(3), f::Irrational{:π})::Expr
# meta: pop location
$(Expr(:inbounds, :pop))
return (StringLiterals.print)(SSAValue(26), ", 0x", SSAValue(21), ", ", SSAValue(16), ", <", SSAValue(14), ">, <", SSAValue(9), ">, ", $(Expr(:foreigncall, :(:jl_toplevel_eval_in), Any, svec(Any, Any), :(Format.Format), 0, SSAValue(2), 0)), ", ", $(Expr(:invoke, MethodInstance for #fmt#13(::Array{Any,1}, ::Function, ::Rational{Int64}), :(Format.#fmt#13), :($(Expr(:foreigncall, :(:jl_alloc_array_1d), Array{Any,1}, svec(Any, Int64), Array{Any,1}, 0, 0, 0))), :(StringLiterals.fmt), :(r))), "\n")::Void
end::Void
julia> prf(nam, i, f, r) = (@printf("%-5d, 0x%x, %d, <%-10s>, <%10s>, %7.3f, %f\n",i, i, i, nam, nam, f, r))
prf (generic function with 1 method)
julia> @code_warntype prf("scott", 42, pi, 1//200)
Variables:
#self# <optimized out>
nam::String
i::Int64
f <optimized out>
r::Rational{Int64}
args::Tuple{Int32,Int32,Bool}
do_out::Bool
exp <optimized out>
len::Int32
pt::Int32
neg::Bool
out::Any
##x#687 <optimized out>
##x#689 <optimized out>
##x#690 <optimized out>
##x#691::String
##x#693::String
##x#695 <optimized out>
##x#697 <optimized out>
#temp#@_20 <optimized out>
#temp#@_21 <optimized out>
##i#688::Int64
#temp#@_23 <optimized out>
#temp#@_24 <optimized out>
#temp#@_25 <optimized out>
#temp#@_26 <optimized out>
##i#692::Int64
##i#694::Int64
#temp#@_29 <optimized out>
#temp#@_30 <optimized out>
##i#696::Int64
#temp#@_32 <optimized out>
#temp#@_33 <optimized out>
#temp#@_34::Union{Int32, Int64}
#temp#@_35 <optimized out>
#temp#@_36 <optimized out>
#temp#@_37 <optimized out>
#temp#@_38 <optimized out>
#temp#@_39 <optimized out>
#temp#@_40 <optimized out>
#temp#@_41::Union{Int32, Int64}
#temp#@_42 <optimized out>
#temp#@_43 <optimized out>
#temp#@_44::String
#temp#@_45::String
#temp#@_46::Core.MethodInstance
#temp#@_47::Int64
#temp#@_48::Core.MethodInstance
#temp#@_49::Int64
Body:
begin
NewvarNode(:(args::Tuple{Int32,Int32,Bool}))
NewvarNode(:(do_out::Bool))
NewvarNode(:(len::Int32))
NewvarNode(:(pt::Int32))
NewvarNode(:(neg::Bool))
NewvarNode(:(##i#688::Int64))
NewvarNode(:(##i#692::Int64))
NewvarNode(:(##i#694::Int64))
NewvarNode(:(##i#696::Int64))
out::Any = Base.Printf.STDOUT
##x#691::String = nam::String
##x#693::String = nam::String
SSAValue(42) = $(Expr(:invoke, MethodInstance for decode_dec(::Int64), :(Base.Printf.decode_dec), :(i)))
SSAValue(43) = (Base.add_int)(1, 1)::Int64
do_out::Bool = true
SSAValue(44) = (Base.add_int)(2, 1)::Int64
args::Tuple{Int32,Int32,Bool} = SSAValue(42)
unless do_out::Bool goto 68
SSAValue(45) = (Base.getfield)(args::Tuple{Int32,Int32,Bool}, 1)::Int32
SSAValue(46) = (Base.add_int)(1, 1)::Int64
len::Int32 = SSAValue(45)
SSAValue(47) = (Base.getfield)(args::Tuple{Int32,Int32,Bool}, 2)::Int32
SSAValue(48) = (Base.add_int)(2, 1)::Int64
pt::Int32 = SSAValue(47)
SSAValue(49) = (Base.getfield)(args::Tuple{Int32,Int32,Bool}, 3)::Bool
SSAValue(50) = (Base.add_int)(3, 1)::Int64
neg::Bool = SSAValue(49)
unless neg::Bool goto 31
(Base.Printf.write)(out::Any, '-')::Any
goto 31
31:
(Base.Printf.unsafe_write)(out::Any, $(Expr(:foreigncall, :(:jl_array_ptr), Ptr{UInt8}, svec(Any), :(Base.Printf.DIGITS), 0)), pt::Int32)::Any
# meta: location printf.jl # line 159:
unless (Base.slt_int)(1, (Base.sext_int)(Int64, pt::Int32)::Int64)::Bool goto 38
#temp#@_34::Union{Int32, Int64} = pt::Int32
goto 40
38:
#temp#@_34::Union{Int32, Int64} = 1
40:
unless (#temp#@_34::Union{Int32, Int64} isa Int32)::Bool goto 44
#temp#@_46::Core.MethodInstance = MethodInstance for -(::Int64, ::Int32)
goto 53
44:
unless (#temp#@_34::Union{Int32, Int64} isa Int64)::Bool goto 48
#temp#@_46::Core.MethodInstance = MethodInstance for -(::Int64, ::Int64)
goto 53
48:
goto 50
50:
#temp#@_47::Int64 = ((Base.sub_int)(5, (Base.and_int)((Base.zext_int)(Int64, neg::Bool)::Int64, 1)::Int64)::Int64 - #temp#@_34::Union{Int32, Int64})::Int64
goto 55
53:
#temp#@_47::Int64 = $(Expr(:invoke, :(#temp#@_46), :(Base.Printf.-), :((Base.sub_int)(5, (Base.and_int)((Base.zext_int)(Int64, neg)::Int64, 1)::Int64)::Int64), :(#temp#@_34)))
55:
##i#688::Int64 = #temp#@_47::Int64 # line 160:
58:
unless (Base.slt_int)(0, ##i#688::Int64)::Bool goto 66 # line 161:
(Base.Printf.write)(out::Any, ' ')::Any # line 162:
##i#688::Int64 = (Base.sub_int)(##i#688::Int64, 1)::Int64
64:
goto 58
66:
# meta: pop location
68:
goto 70
70:
(Base.Printf.write)(out::Any, ", 0x")::Any
SSAValue(51) = $(Expr(:invoke, MethodInstance for decode_hex(::Int64, ::Array{UInt8,1}), :(Base.Printf.decode_hex), :(i), :(Base.Printf.hex_symbols)))
SSAValue(52) = (Base.add_int)(1, 1)::Int64
do_out::Bool = true
SSAValue(53) = (Base.add_int)(2, 1)::Int64
args::Tuple{Int32,Int32,Bool} = SSAValue(51)
unless do_out::Bool goto 92
SSAValue(54) = (Base.getfield)(args::Tuple{Int32,Int32,Bool}, 1)::Int32
SSAValue(55) = (Base.add_int)(1, 1)::Int64
len::Int32 = SSAValue(54)
SSAValue(56) = (Base.getfield)(args::Tuple{Int32,Int32,Bool}, 2)::Int32
SSAValue(57) = (Base.add_int)(2, 1)::Int64
pt::Int32 = SSAValue(56)
SSAValue(58) = (Base.getfield)(args::Tuple{Int32,Int32,Bool}, 3)::Bool
SSAValue(59) = (Base.add_int)(3, 1)::Int64
neg::Bool = SSAValue(58)
unless neg::Bool goto 90
(Base.Printf.write)(out::Any, '-')::Any
goto 90
90:
(Base.Printf.unsafe_write)(out::Any, $(Expr(:foreigncall, :(:jl_array_ptr), Ptr{UInt8}, svec(Any), :(Base.Printf.DIGITS), 0)), pt::Int32)::Any
92:
goto 94
94:
(Base.Printf.write)(out::Any, ", ")::Any
SSAValue(60) = $(Expr(:invoke, MethodInstance for decode_dec(::Int64), :(Base.Printf.decode_dec), :(i)))
SSAValue(61) = (Base.add_int)(1, 1)::Int64
do_out::Bool = true
SSAValue(62) = (Base.add_int)(2, 1)::Int64
args::Tuple{Int32,Int32,Bool} = SSAValue(60)
unless do_out::Bool goto 116
SSAValue(63) = (Base.getfield)(args::Tuple{Int32,Int32,Bool}, 1)::Int32
SSAValue(64) = (Base.add_int)(1, 1)::Int64
len::Int32 = SSAValue(63)
SSAValue(65) = (Base.getfield)(args::Tuple{Int32,Int32,Bool}, 2)::Int32
SSAValue(66) = (Base.add_int)(2, 1)::Int64
pt::Int32 = SSAValue(65)
SSAValue(67) = (Base.getfield)(args::Tuple{Int32,Int32,Bool}, 3)::Bool
SSAValue(68) = (Base.add_int)(3, 1)::Int64
neg::Bool = SSAValue(67)
unless neg::Bool goto 114
(Base.Printf.write)(out::Any, '-')::Any
goto 114
114:
(Base.Printf.unsafe_write)(out::Any, $(Expr(:foreigncall, :(:jl_array_ptr), Ptr{UInt8}, svec(Any), :(Base.Printf.DIGITS), 0)), pt::Int32)::Any
116:
goto 118
118:
(Base.Printf.write)(out::Any, ", <")::Any
##x#691::String = $(Expr(:invoke, MethodInstance for string(::String, ::Vararg{String,N} where N), :(Base.Printf.string), :(##x#691)))
(Base.Printf.write)(out::Any, ##x#691::String)::Any
# meta: location printf.jl # line 159:
SSAValue(40) = $(Expr(:invoke, MethodInstance for strwidth(::String), :(Base.Printf.strwidth), :(##x#691)))
##i#692::Int64 = (Base.sub_int)(10, SSAValue(40))::Int64 # line 160:
127:
unless (Base.slt_int)(0, ##i#692::Int64)::Bool goto 135 # line 161:
(Base.Printf.write)(out::Any, ' ')::Any # line 162:
##i#692::Int64 = (Base.sub_int)(##i#692::Int64, 1)::Int64
133:
goto 127
135:
# meta: pop location
(Base.Printf.write)(out::Any, ">, <")::Any
##x#693::String = $(Expr(:invoke, MethodInstance for string(::String, ::Vararg{String,N} where N), :(Base.Printf.string), :(##x#693)))
# meta: location printf.jl # line 159:
SSAValue(41) = $(Expr(:invoke, MethodInstance for strwidth(::String), :(Base.Printf.strwidth), :(##x#693)))
##i#694::Int64 = (Base.sub_int)(10, SSAValue(41))::Int64 # line 160:
144:
unless (Base.slt_int)(0, ##i#694::Int64)::Bool goto 152 # line 161:
(Base.Printf.write)(out::Any, ' ')::Any # line 162:
##i#694::Int64 = (Base.sub_int)(##i#694::Int64, 1)::Int64
150:
goto 144
152:
# meta: pop location
(Base.Printf.write)(out::Any, ##x#693::String)::Any
(Base.Printf.write)(out::Any, ">, ")::Any
SSAValue(69) = $(Expr(:invoke, MethodInstance for fix_dec(::Float64, ::Int64), :(Base.Printf.fix_dec), 3.141592653589793, 3))
SSAValue(70) = (Base.add_int)(1, 1)::Int64
do_out::Bool = true
SSAValue(71) = (Base.add_int)(2, 1)::Int64
args::Tuple{Int32,Int32,Bool} = SSAValue(69)
unless do_out::Bool goto 211
SSAValue(72) = (Base.getfield)(args::Tuple{Int32,Int32,Bool}, 1)::Int32
SSAValue(73) = (Base.add_int)(1, 1)::Int64
len::Int32 = SSAValue(72)
SSAValue(74) = (Base.getfield)(args::Tuple{Int32,Int32,Bool}, 2)::Int32
SSAValue(75) = (Base.add_int)(2, 1)::Int64
pt::Int32 = SSAValue(74)
SSAValue(76) = (Base.getfield)(args::Tuple{Int32,Int32,Bool}, 3)::Bool
SSAValue(77) = (Base.add_int)(3, 1)::Int64
neg::Bool = SSAValue(76)
# meta: location printf.jl # line 159:
unless (Base.slt_int)(0, (Base.sext_int)(Int64, pt::Int32)::Int64)::Bool goto 176
#temp#@_41::Union{Int32, Int64} = pt::Int32
goto 178
176:
#temp#@_41::Union{Int32, Int64} = 1
178:
unless (#temp#@_41::Union{Int32, Int64} isa Int32)::Bool goto 182
#temp#@_48::Core.MethodInstance = MethodInstance for -(::Int64, ::Int32)
goto 191
182:
unless (#temp#@_41::Union{Int32, Int64} isa Int64)::Bool goto 186
#temp#@_48::Core.MethodInstance = MethodInstance for -(::Int64, ::Int64)
goto 191
186:
goto 188
188:
#temp#@_49::Int64 = (3 - #temp#@_41::Union{Int32, Int64})::Int64
goto 193
191:
#temp#@_49::Int64 = $(Expr(:invoke, :(#temp#@_48), :(Base.Printf.-), 3, :(#temp#@_41)))
193:
##i#696::Int64 = (Base.sub_int)(#temp#@_49::Int64, (Base.and_int)((Base.zext_int)(Int64, neg::Bool)::Int64, 1)::Int64)::Int64 # line 160:
196:
unless (Base.slt_int)(0, ##i#696::Int64)::Bool goto 204 # line 161:
(Base.Printf.write)(out::Any, ' ')::Any # line 162:
##i#696::Int64 = (Base.sub_int)(##i#696::Int64, 1)::Int64
202:
goto 196
204:
# meta: pop location
unless neg::Bool goto 209
(Base.Printf.write)(out::Any, '-')::Any
goto 209
209:
(Base.Printf.print_fixed)(out::Any, 3, pt::Int32, len::Int32, true)::Void
211:
goto 213
213:
(Base.Printf.write)(out::Any, ", ")::Any
unless (Base.not_int)(((Core.getfield)(r::Rational{Int64}, :den)::Int64 === 0)::Bool)::Bool goto 238
SSAValue(78) = $(Expr(:invoke, MethodInstance for fix_dec(::Float64, ::Int64), :(Base.Printf.fix_dec), :((Base.div_float)((Base.sitofp)(Float64, (Core.getfield)(r, :num)::Int64)::Float64, (Base.sitofp)(Float64, (Core.getfield)(r, :den)::Int64)::Float64)::Float64), 6))
SSAValue(79) = (Base.add_int)(1, 1)::Int64
do_out::Bool = true
SSAValue(80) = (Base.add_int)(2, 1)::Int64
args::Tuple{Int32,Int32,Bool} = SSAValue(78)
unless do_out::Bool goto 236
SSAValue(81) = (Base.getfield)(args::Tuple{Int32,Int32,Bool}, 1)::Int32
SSAValue(82) = (Base.add_int)(1, 1)::Int64
len::Int32 = SSAValue(81)
SSAValue(83) = (Base.getfield)(args::Tuple{Int32,Int32,Bool}, 2)::Int32
SSAValue(84) = (Base.add_int)(2, 1)::Int64
pt::Int32 = SSAValue(83)
SSAValue(85) = (Base.getfield)(args::Tuple{Int32,Int32,Bool}, 3)::Bool
SSAValue(86) = (Base.add_int)(3, 1)::Int64
neg::Bool = SSAValue(85)
unless neg::Bool goto 234
(Base.Printf.write)(out::Any, '-')::Any
goto 234
234:
(Base.Printf.print_fixed)(out::Any, 6, pt::Int32, len::Int32, true)::Void
236:
goto 253
238:
# meta: location printf.jl # line 145:
goto 242
242:
unless (Base.slt_int)((Base.sext_int)(Int128, (Core.getfield)(r::Rational{Int64}, :num)::Int64)::Int128, (Base.mul_int)((Base.sext_int)(Int128, (Core.getfield)(r::Rational{Int64}, :den)::Int64)::Int128, (Base.sext_int)(Int128, 0)::Int128)::Int128)::Bool goto 246
#temp#@_45::String = "-Inf"
goto 248
246:
#temp#@_45::String = "Inf"
248:
#temp#@_44::String = #temp#@_45::String
250:
# meta: pop location
(Base.Printf.write)(out::Any, #temp#@_44::String)::Any
253:
(Base.Printf.write)(out::Any, '\n')::Any
return Base.Printf.nothing
end::Void
julia> prx(nam,i,f,r) = pr"\%-5d(i), 0x\%x(i), \%(i), <\%-10s(nam)>, <\%10s(nam)>, \%7.3f(f), \%(r)\n"
prx (generic function with 1 method)
julia> @code_warntype prx("scott", 42, pi, 1//200)
Variables:
#self# <optimized out>
nam::String
i::Int64
f::Irrational{:π}
r::Rational{Int64}
Body:
begin
$(Expr(:inbounds, false))
# meta: location /Users/scott/.julia/v0.6/Format/src/cformat.jl cfmt 6
SSAValue(24) = $(Expr(:invoke, MethodInstance for generate_formatter(::String), :(Format.generate_formatter), "%-5d"))
SSAValue(23) = (Core._expr)(:call, SSAValue(24), i::Int64)::Expr
# meta: pop location
$(Expr(:inbounds, :pop))
SSAValue(26) = $(Expr(:foreigncall, :(:jl_toplevel_eval_in), Any, svec(Any, Any), :(Format.Format), 0, SSAValue(23), 0))
$(Expr(:inbounds, false))
# meta: location /Users/scott/.julia/v0.6/Format/src/cformat.jl cfmt 6
SSAValue(19) = $(Expr(:invoke, MethodInstance for generate_formatter(::String), :(Format.generate_formatter), "%x"))
SSAValue(18) = (Core._expr)(:call, SSAValue(19), i::Int64)::Expr
# meta: pop location
$(Expr(:inbounds, :pop))
SSAValue(21) = $(Expr(:foreigncall, :(:jl_toplevel_eval_in), Any, svec(Any, Any), :(Format.Format), 0, SSAValue(18), 0))
SSAValue(16) = $(Expr(:invoke, MethodInstance for #fmt#13(::Array{Any,1}, ::Function, ::Int64), :(Format.#fmt#13), :($(Expr(:foreigncall, :(:jl_alloc_array_1d), Array{Any,1}, svec(Any, Int64), Array{Any,1}, 0, 0, 0))), :(StringLiterals.fmt), :(i)))
$(Expr(:inbounds, false))
# meta: location /Users/scott/.julia/v0.6/Format/src/cformat.jl cfmt 6
SSAValue(12) = $(Expr(:invoke, MethodInstance for generate_formatter(::String), :(Format.generate_formatter), "%-10s"))
SSAValue(11) = (Core._expr)(:call, SSAValue(12), nam::String)::Expr
# meta: pop location
$(Expr(:inbounds, :pop))
SSAValue(14) = $(Expr(:foreigncall, :(:jl_toplevel_eval_in), Any, svec(Any, Any), :(Format.Format), 0, SSAValue(11), 0))
$(Expr(:inbounds, false))
# meta: location /Users/scott/.julia/v0.6/Format/src/cformat.jl cfmt 6
SSAValue(7) = $(Expr(:invoke, MethodInstance for generate_formatter(::String), :(Format.generate_formatter), "%10s"))
SSAValue(6) = (Core._expr)(:call, SSAValue(7), nam::String)::Expr
# meta: pop location
$(Expr(:inbounds, :pop))
SSAValue(9) = $(Expr(:foreigncall, :(:jl_toplevel_eval_in), Any, svec(Any, Any), :(Format.Format), 0, SSAValue(6), 0))
$(Expr(:inbounds, false))
# meta: location /Users/scott/.julia/v0.6/Format/src/cformat.jl cfmt 6
SSAValue(3) = $(Expr(:invoke, MethodInstance for generate_formatter(::String), :(Format.generate_formatter), "%7.3f"))
SSAValue(2) = (Core._expr)(:call, SSAValue(3), f::Irrational{:π})::Expr
# meta: pop location
$(Expr(:inbounds, :pop))
return (StringLiterals.print)(SSAValue(26), ", 0x", SSAValue(21), ", ", SSAValue(16), ", <", SSAValue(14), ">, <", SSAValue(9), ">, ", $(Expr(:foreigncall, :(:jl_toplevel_eval_in), Any, svec(Any, Any), :(Format.Format), 0, SSAValue(2), 0)), ", ", $(Expr(:invoke, MethodInstance for #fmt#13(::Array{Any,1}, ::Function, ::Rational{Int64}), :(Format.#fmt#13), :($(Expr(:foreigncall, :(:jl_alloc_array_1d), Array{Any,1}, svec(Any, Int64), Array{Any,1}, 0, 0, 0))), :(StringLiterals.fmt), :(r))), "\n")::Void
end::Void
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment