# Why is plotting with GR faster when using GR\_jll and \`@ccall\` directly

**URL:** <https://discourse.julialang.org/t/why-is-plotting-with-gr-faster-when-using-gr-jll-and-ccall-directly/58199>\
**Category:** Performance\
**Tags:** plotting, gr\
**Created:** [March 29, 2021, 11:37pm UTC](https://discourse.julialang.org/t/why-is-plotting-with-gr-faster-when-using-gr-jll-and-ccall-directly/58199 "2021-03-29T23:37:56Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [March 29, 2021, 11:37pm UTC](https://discourse.julialang.org/t/why-is-plotting-with-gr-faster-when-using-gr-jll-and-ccall-directly/58199/1 "2021-03-29T23:37:56Z")

</div>

Why is plotting with GR faster when using GR\_jll and `@ccall` directly? @jheinen

# using GR\_jll

```julia
   _ _ _(_)_ | Documentation: https://docs.julialang.org
  (_) | (_) (_) |
   _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` | |
  | | |_| | | | (_| | | Version 1.6.0 (2021-03-24)
 _/ |\ __'_|_|_|\__'_| | Official https://julialang.org/ release
|__/ |

julia> @time begin
           @time using GR_jll
           x = [0, 0.2, 0.4, 0.6, 0.8, 1.0]; y = [0.3, 0.5, 0.4, 0.2, 0.6, 0.7]
           gr = :libgr
           @time @ccall gr.gr_polyline(6::Cint, pointer(x)::Ptr{Float64}, pointer(y)::Ptr{Float64})::Nothing
           @time @ccall gr.gr_axes(0.2::Float64, 0.2::Float64, 0::Float64, 0::Float64, 1::Cint, 1::Cint, (-0.01)::Float64)::Nothing
           @time @ccall gr.gr_updatews()::Nothing
       end
  0.681576 seconds (292.18 k allocations: 19.435 MiB, 0.77% gc time, 0.48% compilation time) # using GR_jll
GKS: file open error (/workspace/destdir\FONTS\GKSFONT.DAT)
open: No such file or directory
  0.033260 seconds (3.92 k allocations: 223.016 KiB, 30.53% compilation time) # gr.gr_polyline
  0.013906 seconds (3.53 k allocations: 203.047 KiB, 63.91% compilation time) # gr.gr_axes
  0.000004 seconds (6 allocations: 192 bytes) # gr.gr_updatews
  0.803224 seconds (416.77 k allocations: 27.419 MiB, 0.66% gc time, 2.78% compilation time) # begin ... end

```

versus

# using GR

```julia
   _ _ _(_)_ | Documentation: https://docs.julialang.org
  (_) | (_) (_) |
   _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` | |
  | | |_| | | | (_| | | Version 1.6.0 (2021-03-24)
 _/ |\ __'_|_|_|\__'_| | Official https://julialang.org/ release
|__/ |

julia> @time begin
           @time using GR
           x = [0, 0.2, 0.4, 0.6, 0.8, 1.0]; y = [0.3, 0.5, 0.4, 0.2, 0.6, 0.7]
           gr = :libgr
           @time @ccall gr.gr_polyline(6::Cint, pointer(x)::Ptr{Float64}, pointer(y)::Ptr{Float64})::Nothing
           @time @ccall gr.gr_axes(0.2::Float64, 0.2::Float64, 0::Float64, 0::Float64, 1::Cint, 1::Cint, (-0.01)::Float64)::Nothing
           @time @ccall gr.gr_updatews()::Nothing
       end
  0.237283 seconds (63.35 k allocations: 5.483 MiB, 1.55% compilation time) # using GR
  2.321641 seconds (3.92 k allocations: 223.016 KiB, 0.46% compilation time) # gr.gr_polyline
  0.007653 seconds (3.53 k allocations: 203.047 KiB, 98.55% compilation time) # gr.gr_axes
  0.000056 seconds (6 allocations: 192 bytes) # gr.gr_updatews
  2.638014 seconds (187.92 k allocations: 13.304 MiB, 0.83% compilation time) # begin... end

```

```julia-auto
julia> versioninfo()
Julia Version 1.6.0
Commit f9720dc2eb (2021-03-24 12:55 UTC)
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-11.0.1 (ORCJIT, skylake)

```

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [March 29, 2021, 11:55pm UTC](https://discourse.julialang.org/t/why-is-plotting-with-gr-faster-when-using-gr-jll-and-ccall-directly/58199/2 "2021-03-29T23:55:56Z")

</div>

# `GR. __init__ ` changes Environmental Variables

I took a look at GR.jl’s ` __init__ ` and I noticed it set some environmental variables:

```julia
julia> orgENV= copy(ENV);

julia> using GR

julia> setdiff(ENV,orgENV)
4-element Vector{Pair{String, String}}:
      "GKS_ENCODING" => "utf8"
      "GKS_FONTPATH" => "C:\\Users\\kittisopikulm\\.julia\\packages\\GR\\BwGt2\\src\\..\\deps\\gr"
 "GKS_USE_CAIRO_PNG" => "true"
             "GRDIR" => "C:\\Users\\kittisopikulm\\.julia\\packages\\GR\\BwGt2\\src\\..\\deps\\gr"

```

# Setting environmental variable results in slowdown

```julia
julia> begin
           ENV["GRDIR"] = "C:\\Users\\kittisopikulm\\.julia\\packages\\GR\\BwGt2\\src\\..\\deps\\gr"
           ENV["GKS_FONTPATH"] = "C:\\Users\\kittisopikulm\\.julia\\packages\\GR\\BwGt2\\src\\..\\deps\\gr"
           ENV["GKS_ENCODING"] = "utf8"
           ENV["GKS_USE_CAIRO_PNG"] = "true"
       end
"true"

julia> @time begin
           @time using GR_jll
           x = [0, 0.2, 0.4, 0.6, 0.8, 1.0]; y = [0.3, 0.5, 0.4, 0.2, 0.6, 0.7]
           gr = :libgr
           @time @ccall gr.gr_polyline(6::Cint, pointer(x)::Ptr{Float64}, pointer(y)::Ptr{Float64})::Nothing
           @time @ccall gr.gr_axes(0.2::Float64, 0.2::Float64, 0::Float64, 0::Float64, 1::Cint, 1::Cint, (-0.01)::Float64)::Nothing
           @time @ccall gr.gr_updatews()::Nothing
       end
  0.741127 seconds (292.18 k allocations: 19.435 MiB, 0.92% gc time, 0.46% compilation time)
  2.322584 seconds (3.92 k allocations: 223.016 KiB, 0.46% compilation time)
  0.012314 seconds (3.53 k allocations: 203.047 KiB, 99.16% compilation time)
  0.000056 seconds (6 allocations: 192 bytes)
  3.149256 seconds (416.77 k allocations: 27.419 MiB, 0.22% gc time, 0.83% compilation time)

```

I actually get a blank plot at the end of this, but now it takes more than two seconds to call `gr.gr_polyline`.

# Setting ENV[“GRDIR”] is sufficient to cause the slowdown

```julia
julia> begin
           ENV["GRDIR"] = "C:\\Users\\kittisopikulm\\.julia\\packages\\GR\\BwGt2\\src\\..\\deps\\gr"
       end
"C:\\Users\\kittisopikulm\\.julia\\packages\\GR\\BwGt2\\src\\..\\deps\\gr"

julia> @time begin
           @time using GR_jll
           x = [0, 0.2, 0.4, 0.6, 0.8, 1.0]; y = [0.3, 0.5, 0.4, 0.2, 0.6, 0.7]
           gr = :libgr
           @time @ccall gr.gr_polyline(6::Cint, pointer(x)::Ptr{Float64}, pointer(y)::Ptr{Float64})::Nothing
           @time @ccall gr.gr_axes(0.2::Float64, 0.2::Float64, 0::Float64, 0::Float64, 1::Cint, 1::Cint, (-0.01)::Float64)::Nothing
           @time @ccall gr.gr_updatews()::Nothing
       end
  0.678470 seconds (292.17 k allocations: 19.434 MiB, 0.61% compilation time)
  2.325419 seconds (3.92 k allocations: 223.016 KiB, 0.45% compilation time)
  0.025172 seconds (3.53 k allocations: 203.047 KiB, 99.14% compilation time)
  0.000110 seconds (6 allocations: 192 bytes)
  3.106615 seconds (416.75 k allocations: 27.301 MiB, 1.28% compilation time)

```

# Setting other environmental variables does not result in a slowdown

```julia
julia> begin
           #ENV["GRDIR"] = "C:\\Users\\kittisopikulm\\.julia\\packages\\GR\\BwGt2\\src\\..\\deps\\gr"
           ENV["GKS_FONTPATH"] = "C:\\Users\\kittisopikulm\\.julia\\packages\\GR\\BwGt2\\src\\..\\deps\\gr"
           ENV["GKS_ENCODING"] = "utf8"
           ENV["GKS_USE_CAIRO_PNG"] = "true"
       end
"true"

julia> @time begin
           @time using GR_jll
           x = [0, 0.2, 0.4, 0.6, 0.8, 1.0]; y = [0.3, 0.5, 0.4, 0.2, 0.6, 0.7]
           gr = :libgr
           @time @ccall gr.gr_polyline(6::Cint, pointer(x)::Ptr{Float64}, pointer(y)::Ptr{Float64})::Nothing
           @time @ccall gr.gr_axes(0.2::Float64, 0.2::Float64, 0::Float64, 0::Float64, 1::Cint, 1::Cint, (-0.01)::Float64)::Nothing
           @time @ccall gr.gr_updatews()::Nothing
       end
  0.672858 seconds (292.17 k allocations: 19.434 MiB, 0.46% compilation time)
  0.027195 seconds (3.92 k allocations: 223.016 KiB, 38.21% compilation time)
  0.014427 seconds (3.53 k allocations: 203.047 KiB, 57.92% compilation time)
  0.000004 seconds (6 allocations: 192 bytes)
  0.788307 seconds (416.75 k allocations: 27.350 MiB, 2.77% compilation time)

```

# Package versions

Also here are the versions I am using:

```julia
(@v1.6) pkg> st GR
      Status `C:\Users\kittisopikulm\.julia\environments\v1.6\Project.toml`
  [28b8d3ca] GR v0.55.0

(@v1.6) pkg> st GR_jll
      Status `C:\Users\kittisopikulm\.julia\environments\v1.6\Project.toml`
  [d2c73de3] GR_jll v0.56.1+0

```

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [March 30, 2021, 12:03am UTC](https://discourse.julialang.org/t/why-is-plotting-with-gr-faster-when-using-gr-jll-and-ccall-directly/58199/3 "2021-03-30T00:03:21Z")

</div>

Why not using the variable `libGR` defined by both packages instead of using `:libgr` which doesn’t actually work?

```julia
julia> using GR

julia> x = [0, 0.2, 0.4, 0.6, 0.8, 1.0]; y = [0.3, 0.5, 0.4, 0.2, 0.6, 0.7];

julia> gr = :libgr;

julia> @ccall gr.gr_polyline(6::Cint, pointer(x)::Ptr{Float64}, pointer(y)::Ptr{Float64})::Nothing
ERROR: could not load library "libgr"
libgr.so: cannot open shared object file: No such file or directory
Stacktrace:
 [1] top-level scope
   @ ./REPL[6]:1

```

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [March 30, 2021, 12:06am UTC](https://discourse.julialang.org/t/why-is-plotting-with-gr-faster-when-using-gr-jll-and-ccall-directly/58199/4 "2021-03-30T00:06:13Z")

</div>

Also, I can’t reproduce your timings:

```julia
julia> @time begin
           @time using GR: libGR
           x = [0, 0.2, 0.4, 0.6, 0.8, 1.0]; y = [0.3, 0.5, 0.4, 0.2, 0.6, 0.7]
           @time @ccall libGR.gr_polyline(6::Cint, pointer(x)::Ptr{Float64}, pointer(y)::Ptr{Float64})::Nothing
           @time @ccall libGR.gr_axes(0.2::Float64, 0.2::Float64, 0::Float64, 0::Float64, 1::Cint, 1::Cint, (-0.01)::Float64)::Nothing
           @time @ccall libGR.gr_updatews()::Nothing
       end
  0.146296 seconds (57.11 k allocations: 5.017 MiB, 2.73% compilation time)
  0.315977 seconds (4.04 k allocations: 228.562 KiB, 4.74% compilation time)
  0.011421 seconds (3.62 k allocations: 206.984 KiB, 98.81% compilation time)
  0.000037 seconds (6 allocations: 192 bytes)
  0.574396 seconds (182.01 k allocations: 12.853 MiB, 5.27% compilation time)

```

```julia
julia> @time begin
           @time using GR_jll: libGR
           x = [0, 0.2, 0.4, 0.6, 0.8, 1.0]; y = [0.3, 0.5, 0.4, 0.2, 0.6, 0.7]
           @time @ccall libGR.gr_polyline(6::Cint, pointer(x)::Ptr{Float64}, pointer(y)::Ptr{Float64})::Nothing
           @time @ccall libGR.gr_axes(0.2::Float64, 0.2::Float64, 0::Float64, 0::Float64, 1::Cint, 1::Cint, (-0.01)::Float64)::Nothing
           @time @ccall libGR.gr_updatews()::Nothing
       end
  0.115333 seconds (198.66 k allocations: 15.067 MiB, 3.71% compilation time)
GKS: file open error (/workspace/destdir/fonts/gksfont.dat)
open: No such file or directory
GKS: X11 support not compiled in
  0.016375 seconds (4.04 k allocations: 228.562 KiB, 96.41% compilation time)
  0.010168 seconds (3.62 k allocations: 206.984 KiB, 98.54% compilation time)
  0.000006 seconds (6 allocations: 192 bytes)
  0.246544 seconds (323.58 k allocations: 22.975 MiB, 12.20% compilation time)

```

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [March 30, 2021, 12:09am UTC](https://discourse.julialang.org/t/why-is-plotting-with-gr-faster-when-using-gr-jll-and-ccall-directly/58199/5 "2021-03-30T00:09:59Z")

</div>

> [@giordano](#):
>
> Why not using the variable `libGR` defined by both packages instead of using `:libgr` which doesn’t actually work?

It worked for me, but here we go:

# Using GR; with gr = GR.libGR

```julia
julia> @time begin
           @time using GR
           x = [0, 0.2, 0.4, 0.6, 0.8, 1.0]; y = [0.3, 0.5, 0.4, 0.2, 0.6, 0.7]
           gr = GR.libGR
           @time @ccall gr.gr_polyline(6::Cint, pointer(x)::Ptr{Float64}, pointer(y)::Ptr{Float64})::Nothing
           @time @ccall gr.gr_axes(0.2::Float64, 0.2::Float64, 0::Float64, 0::Float64, 1::Cint, 1::Cint, (-0.01)::Float64)::Nothing
           @time @ccall gr.gr_updatews()::Nothing
       end
  0.244950 seconds (65.85 k allocations: 5.683 MiB, 1.14% compilation time)
  2.325786 seconds (3.92 k allocations: 223.016 KiB, 0.45% compilation time)
  0.021604 seconds (3.53 k allocations: 203.047 KiB, 99.28% compilation time)
  0.000073 seconds (6 allocations: 192 bytes)
  2.664028 seconds (190.41 k allocations: 13.505 MiB, 1.30% compilation time)

```

# using GR\_jll; gr = GR\_jll.libGR

```julia
julia> @time begin
           @time using GR_jll
           x = [0, 0.2, 0.4, 0.6, 0.8, 1.0]; y = [0.3, 0.5, 0.4, 0.2, 0.6, 0.7]
           gr = GR_jll.libGR
           @time @ccall gr.gr_polyline(6::Cint, pointer(x)::Ptr{Float64}, pointer(y)::Ptr{Float64})::Nothing
           @time @ccall gr.gr_axes(0.2::Float64, 0.2::Float64, 0::Float64, 0::Float64, 1::Cint, 1::Cint, (-0.01)::Float64)::Nothing
           @time @ccall gr.gr_updatews()::Nothing
       end
  0.677641 seconds (292.17 k allocations: 19.434 MiB, 0.96% gc time, 0.48% compilation time)
GKS: file open error (/workspace/destdir\FONTS\GKSFONT.DAT)
open: No such file or directory
  0.026946 seconds (3.92 k allocations: 223.016 KiB, 37.90% compilation time)
  0.013350 seconds (3.53 k allocations: 203.047 KiB, 60.41% compilation time)
  0.000006 seconds (6 allocations: 192 bytes)
  0.790913 seconds (416.76 k allocations: 27.418 MiB, 0.82% gc time, 2.72% compilation time)

```

```julia
julia> begin
           #ENV["GRDIR"] = "C:\\Users\\kittisopikulm\\.julia\\packages\\GR\\BwGt2\\src\\..\\deps\\gr"
           ENV["GKS_FONTPATH"] = "C:\\Users\\kittisopikulm\\.julia\\packages\\GR\\BwGt2\\src\\..\\deps\\gr"
           ENV["GKS_ENCODING"] = "utf8"
           ENV["GKS_USE_CAIRO_PNG"] = "true"
       end
"true"

julia> @time begin
           @time using GR_jll
           x = [0, 0.2, 0.4, 0.6, 0.8, 1.0]; y = [0.3, 0.5, 0.4, 0.2, 0.6, 0.7]
           gr = GR_jll.libGR
           @time @ccall gr.gr_polyline(6::Cint, pointer(x)::Ptr{Float64}, pointer(y)::Ptr{Float64})::Nothing
           @time @ccall gr.gr_axes(0.2::Float64, 0.2::Float64, 0::Float64, 0::Float64, 1::Cint, 1::Cint, (-0.01)::Float64)::Nothing
           @time @ccall gr.gr_updatews()::Nothing
       end
  0.677451 seconds (292.17 k allocations: 19.434 MiB, 0.82% gc time, 0.47% compilation time)
  0.034265 seconds (3.92 k allocations: 223.016 KiB, 32.98% compilation time)
  0.012879 seconds (3.53 k allocations: 203.047 KiB, 60.35% compilation time)
  0.000006 seconds (6 allocations: 192 bytes)
  0.797809 seconds (416.76 k allocations: 27.419 MiB, 0.69% gc time, 2.79% compilation time)

```

# using GR\_jll; gr = GR\_jll.libGR; Setting GRDIR environment variable

```julia
julia> begin
           ENV["GRDIR"] = "C:\\Users\\kittisopikulm\\.julia\\packages\\GR\\BwGt2\\src\\..\\deps\\gr"
           #ENV["GKS_FONTPATH"] = "C:\\Users\\kittisopikulm\\.julia\\packages\\GR\\BwGt2\\src\\..\\deps\\gr"
           #ENV["GKS_ENCODING"] = "utf8"
           #ENV["GKS_USE_CAIRO_PNG"] = "true"
       end
"C:\\Users\\kittisopikulm\\.julia\\packages\\GR\\BwGt2\\src\\..\\deps\\gr"

julia> @time begin
           @time using GR_jll
           x = [0, 0.2, 0.4, 0.6, 0.8, 1.0]; y = [0.3, 0.5, 0.4, 0.2, 0.6, 0.7]
           gr = GR_jll.libGR
           @time @ccall gr.gr_polyline(6::Cint, pointer(x)::Ptr{Float64}, pointer(y)::Ptr{Float64})::Nothing
           @time @ccall gr.gr_axes(0.2::Float64, 0.2::Float64, 0::Float64, 0::Float64, 1::Cint, 1::Cint, (-0.01)::Float64)::Nothing
           @time @ccall gr.gr_updatews()::Nothing
       end
  0.680800 seconds (292.17 k allocations: 19.419 MiB, 0.78% gc time, 0.48% compilation time)
  2.323178 seconds (3.92 k allocations: 223.016 KiB, 0.46% compilation time)
  0.025672 seconds (3.53 k allocations: 203.047 KiB, 99.27% compilation time)
  0.000111 seconds (6 allocations: 192 bytes)
  3.104938 seconds (416.76 k allocations: 27.403 MiB, 0.17% gc time, 1.27% compilation time)

```

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [March 30, 2021, 12:22am UTC](https://discourse.julialang.org/t/why-is-plotting-with-gr-faster-when-using-gr-jll-and-ccall-directly/58199/6 "2021-03-30T00:22:22Z")

</div>

What happens if I just delete the `GRDIR` environmental variable?

# Deleting `GRDIR` environmental variable, using `@ccall`

```julia
   _ _ _(_)_ | Documentation: https://docs.julialang.org
  (_) | (_) (_) |
   _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` | |
  | | |_| | | | (_| | | Version 1.6.0 (2021-03-24)
 _/ |\ __'_|_|_|\__'_| | Official https://julialang.org/ release
|__/ |

julia> @time begin
           @time using GR
           delete!(ENV, "GRDIR")
           x = [0, 0.2, 0.4, 0.6, 0.8, 1.0]; y = [0.3, 0.5, 0.4, 0.2, 0.6, 0.7]
           gr = GR.libGR
           @time @ccall gr.gr_polyline(6::Cint, pointer(x)::Ptr{Float64}, pointer(y)::Ptr{Float64})::Nothing
           @time @ccall gr.gr_axes(0.2::Float64, 0.2::Float64, 0::Float64, 0::Float64, 1::Cint, 1::Cint, (-0.01)::Float64)::Nothing
           @time @ccall gr.gr_updatews()::Nothing
       end
  0.243086 seconds (65.84 k allocations: 5.586 MiB, 1.17% compilation time)
  0.023278 seconds (3.92 k allocations: 223.016 KiB, 44.35% compilation time)
  0.011711 seconds (3.53 k allocations: 203.047 KiB, 76.49% compilation time)
  0.000006 seconds (6 allocations: 192 bytes)
  0.351513 seconds (190.47 k allocations: 13.412 MiB, 6.29% compilation time)

```

# Using GR with low level calls, deleting the GRDIR environmental variable

```julia
julia> @time begin
           @time using GR
           delete!(ENV, "GRDIR")
           x = [0, 0.2, 0.4, 0.6, 0.8, 1.0]; y = [0.3, 0.5, 0.4, 0.2, 0.6, 0.7]
           @time GR.polyline(x,y)
           @time GR.axes(0.2, 0.2, 0, 0, 1, 1, -0.01)
           @time GR.updatews()
       end
  0.244590 seconds (65.84 k allocations: 5.667 MiB, 1.18% compilation time)
  0.016647 seconds (4.20 k allocations: 239.353 KiB, 30.95% compilation time)
  0.010972 seconds (7.61 k allocations: 439.749 KiB, 45.82% compilation time)
  0.002287 seconds (499 allocations: 36.671 KiB, 98.66% compilation time)
  0.347401 seconds (195.32 k allocations: 13.777 MiB, 4.41% compilation time)

```

This works, and I get my plot:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/0/3/03c2521b8aff4f9a62771268da3ab67a60f6093f.png)

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [March 30, 2021, 12:26am UTC](https://discourse.julialang.org/t/why-is-plotting-with-gr-faster-when-using-gr-jll-and-ccall-directly/58199/7 "2021-03-30T00:26:35Z")

</div>

Next, I wondered what if I combined this with PackageCompiler.jl and just used `plot`:

```julia
PS C:\Users\kittisopikulm> julia -J .\sys_plots.dll _
   _ _ _(_)_ | Documentation: https://docs.julialang.org
  (_) | (_) (_) |
   _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` | |
  | | |_| | | | (_| | | Version 1.6.0 (2021-03-24)
 _/ |\ __'_|_|_|\__'_| | Official https://julialang.org/ release
|__/ |

julia> @time begin
           @time using GR
           # delete!(ENV, "GRDIR")
           x = [0, 0.2, 0.4, 0.6, 0.8, 1.0]; y = [0.3, 0.5, 0.4, 0.2, 0.6, 0.7]
           plot(x,y)
       end
  0.000945 seconds (811 allocations: 59.977 KiB, 211.65% compilation time)
  2.387969 seconds (125.19 k allocations: 8.038 MiB, 0.08% compilation time)

julia> exit()
PS C:\Users\kittisopikulm> julia -J .\sys_plots.dll
               _
   _ _ _(_)_ | Documentation: https://docs.julialang.org
  (_) | (_) (_) |
   _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` | |
  | | |_| | | | (_| | | Version 1.6.0 (2021-03-24)
 _/ |\ __'_|_|_|\__'_| | Official https://julialang.org/ release
|__/ |

julia> @time begin
           @time using GR
           delete!(ENV, "GRDIR")
           x = [0, 0.2, 0.4, 0.6, 0.8, 1.0]; y = [0.3, 0.5, 0.4, 0.2, 0.6, 0.7]
           plot(x,y)
       end
  0.001334 seconds (811 allocations: 59.977 KiB, 213.11% compilation time)
  0.096102 seconds (125.26 k allocations: 8.043 MiB, 2.96% compilation time)

```

This works, but in this case I’m missing my axis labels:

![image](https://global.discourse-cdn.com/julialang/original/3X/f/9/f925cca2c9f22067cfa66734d052c3e84e72f4ed.png)

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [March 30, 2021, 12:45am UTC](https://discourse.julialang.org/t/why-is-plotting-with-gr-faster-when-using-gr-jll-and-ccall-directly/58199/8 "2021-03-30T00:45:48Z")

</div>

> [@giordano](#):
>
> Also, I can’t reproduce your timings:

Maybe this is a Windows issue?

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [March 30, 2021, 6:30am UTC](https://discourse.julialang.org/t/why-is-plotting-with-gr-faster-when-using-gr-jll-and-ccall-directly/58199/9 "2021-03-30T06:30:00Z")

</div>

Above, `GR.gr_provider` was actually “GR”. This means GR.jl was not actually using GR\_jll at all, but rather downloading GR itself. This is not surprising since GR.jl’s use of BinaryBuilder and GR\_jll was disabled 5 days ago:  
[https://github.com/jheinen/GR.jl/commit/1e6d3d526ed8ca5757f371c618fd41f83a1012c1](https://github.com/jheinen/GR.jl/commit/1e6d3d526ed8ca5757f371c618fd41f83a1012c1)

I can force the use of GR\_jll by

1. `$env:JULIA_GR_PROVIDER="BinaryBuilder"` in PowerShell
2. `using Pkg; Pkg.build("GR")`

Attempting to use GR after this produces errors of this sort:

```julia
julia> @time begin
                  @time using GR: libGR, GR_jll
                  x = [0, 0.2, 0.4, 0.6, 0.8, 1.0]; y = [0.3, 0.5, 0.4, 0.2, 0.6, 0.7]
                  @time @ccall libGR.gr_polyline(6::Cint, pointer(x)::Ptr{Float64}, pointer(y)::Ptr{Float64})::Nothing
                  @time @ccall libGR.gr_axes(0.2::Float64, 0.2::Float64, 0::Float64, 0::Float64, 1::Cint, 1::Cint, (-0.01)::Float64)::Nothing
                  @time @ccall libGR.gr_updatews()::Nothing
              end
  0.808025 seconds (355.11 k allocations: 24.770 MiB, 0.39% compilation time)
connect: No error
GKS: can't connect to GKS socket application

GKS: Open failed in routine OPEN_WS
GKS: GKS not in proper state. GKS must be either in the state WSOP or WSAC in routine ACTIVATE_WS
GKS: GKS not in proper state. GKS must be either in the state WSAC or SGOP in routine POLYLINE
 23.070139 seconds (3.92 k allocations: 223.016 KiB, 0.05% compilation time)
GKS: GKS not in proper state. GKS must be either in the state WSAC or SGOP in routine TEXT
GKS: GKS not in proper state. GKS must be either in the state WSAC or SGOP in routine TEXT
GKS: GKS not in proper state. GKS must be either in the state WSAC or SGOP in routine TEXT
GKS: GKS not in proper state. GKS must be either in the state WSAC or SGOP in routine TEXT
GKS: GKS not in proper state. GKS must be either in the state WSAC or SGOP in routine TEXT
GKS: GKS not in proper state. GKS must be either in the state WSAC or SGOP in routine TEXT
GKS: GKS not in proper state. GKS must be either in the state WSAC or SGOP in routine POLYLINE
GKS: GKS not in proper state. GKS must be either in the state WSAC or SGOP in routine TEXT
GKS: GKS not in proper state. GKS must be either in the state WSAC or SGOP in routine TEXT
GKS: GKS not in proper state. GKS must be either in the state WSAC or SGOP in routine TEXT
GKS: GKS not in proper state. GKS must be either in the state WSAC or SGOP in routine TEXT
GKS: GKS not in proper state. GKS must be either in the state WSAC or SGOP in routine TEXT
GKS: GKS not in proper state. GKS must be either in the state WSAC or SGOP in routine TEXT
GKS: GKS not in proper state. GKS must be either in the state WSAC or SGOP in routine POLYLINE
  0.036716 seconds (3.53 k allocations: 203.047 KiB, 69.96% compilation time)
  0.000008 seconds (6 allocations: 192 bytes)
 23.993162 seconds (479.67 k allocations: 32.592 MiB, 0.17% compilation time)

```

This problem is due to how the `GKS_QT` environmental variable is constructed which attempts to set the PATH and execute a command.

> <https://github.com/jheinen/GR.jl/blob/master/src/GR.jl#L331>

The following resolves the GKS state issue by setting the PATH through Julia first.

```julia
julia> @time begin
                  @time using GR: libGR, GR_jll
                  ENV["PATH"] = GR_jll.LIBPATH[] * ";" * ENV["PATH"]
                  ENV["GKS_QT"] = GR_jll.gksqt_path
                  x = [0, 0.2, 0.4, 0.6, 0.8, 1.0]; y = [0.3, 0.5, 0.4, 0.2, 0.6, 0.7]
                  @time @ccall libGR.gr_polyline(6::Cint, pointer(x)::Ptr{Float64}, pointer(y)::Ptr{Float64})::Nothing
                  @time @ccall libGR.gr_axes(0.2::Float64, 0.2::Float64, 0::Float64, 0::Float64, 1::Cint, 1::Cint, (-0.01)::Float64)::Nothing
                  @time @ccall libGR.gr_updatews()::Nothing
              end
  0.789749 seconds (355.13 k allocations: 24.924 MiB, 0.73% gc time, 0.40% compilation time)
  2.325438 seconds (3.92 k allocations: 223.016 KiB, 0.54% compilation time)
  0.013462 seconds (3.53 k allocations: 203.047 KiB, 98.92% compilation time)
  0.000089 seconds (6 allocations: 192 bytes)
  3.203867 seconds (479.71 k allocations: 32.768 MiB, 0.18% gc time, 0.91% compilation time)

```

However, the we again see the time lag.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [March 30, 2021, 6:57am UTC](https://discourse.julialang.org/t/why-is-plotting-with-gr-faster-when-using-gr-jll-and-ccall-directly/58199/10 "2021-03-30T06:57:01Z")

</div>

By deleting `ENV["GRDIR"]` above, I believe the main effect was to change the practical workstation type, which can be viewed in the help file of `GR.openws`. In my case, we can set `ENV["GKSWSTYPE"] = 41` for Windows GDI to get a fairly fast plot without using a systems image.

```julia
   _ _ _(_)_ | Documentation: https://docs.julialang.org
  (_) | (_) (_) |
   _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` | |
  | | |_| | | | (_| | | Version 1.6.0 (2021-03-24)
 _/ |\ __'_|_|_|\__'_| | Official https://julialang.org/ release
|__/ |

julia> @time begin
           using GR
           ENV["GKSWSTYPE"] = 41
           x = [0, 0.2, 0.4, 0.6, 0.8, 1.0]; y = [0.3, 0.5, 0.4, 0.2, 0.6, 0.7]
           GR.polyline(x,y)
           GR.axes(0.2, 0.2, 0, 0, 1, 1, -0.01)
           GR.updatews()
       end
  0.855669 seconds (412.19 k allocations: 28.426 MiB, 0.63% gc time, 0.37% compilation time)

```
