# Code taking too long to run. Need some advice

**URL:** <https://discourse.julialang.org/t/code-taking-too-long-to-run-need-some-advice/37485>\
**Category:** New to Julia\
**Created:** [April 13, 2020, 3:23am UTC](https://discourse.julialang.org/t/code-taking-too-long-to-run-need-some-advice/37485 "2020-04-13T03:23:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![baba12321](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baba12321/32/14039_2.png) [@baba12321](https://discourse.julialang.org/u/baba12321)\
**Post date:** [April 13, 2020, 3:23am UTC](https://discourse.julialang.org/t/code-taking-too-long-to-run-need-some-advice/37485/1 "2020-04-13T03:23:44Z")

</div>

First time user here and I am working on a basic scheduling of jobs. The data for the code is generated using:

> # Data generator
> 
> n = 20 # number of tasks  
> min\_end = 5 # the min end time of any task  
> max\_end = 15 # the max end time of any task  
> min\_workload = 1 # the min workload of any task  
> max\_workload = 4 # the max workload of any task, here we restrict max\_workload \<= min\_end  
> min\_bonus = 5 # the min bonus  
> max\_bonus = 25 # the max bonus
> 
> function gen\_data(is\_float)  
> if max\_workload \> min\_end  
> error(“Min end time needs to be greater that max workload”)  
> end
> 
> ```
> if is_float == false
> end_time = rand(min_end:max_end, n) # the end time of each task 
> workload = zeros(n)
> for i=1:n
> workload[i] = rand(min_workload:max_workload) # workload
> end
> start_time = end_time - workload # the start time of each task
> bonus = rand(min_bonus:max_bonus, n) # the bonus of each task
> else
> end_time = rand(n)*(max_end - min_end) + min_end # the end time of each task 
> workload = zeros(n)
> for i=1:n
> workload[i] = rand()*(max_workload - min_workload) + min_workload # workload
> end
> start_time = end_time - workload # the start time of each task
> bonus = rand(n)*(max_bonus - min_bonus) + min_bonus # the bonus of each task
> end
> 
> data = cat(2, end_time, workload, start_time, bonus)
> 
> # return it
> return data
> 
> ```
> 
> end

Function where the problem is defined is

> function minNumProcessors(end\_time, start\_time, conflicts)  
> # end\_time: the end time of each task  
> # start\_time: the start time of each task  
> # conflicts: conflicts[i] is the set of tasks whose time conflicts with task i  
> #  
> # returned: (x,y,obj), where  
> # 1. x[i,j] indicates whether task i is scheduled on processor j  
> # 2. y[j] indicates whether processor j is used;  
> # 3. obj is the maximum total bonus
> 
> ```
> num = length(bonus) # num of tasks
> 
> m = Model(GLPK.Optimizer)
> 
> @variable(m, x[1:num, 1:num], Bin) # whether task i is scheduled on processor j
> @variable(m, y[1:num], Bin) # whether processor j is used
> 
> for i=1:num # the set partition constraint
> @constraint(m, sum(x[i,j] for j=1:num) == 1)
> end
> 
> for i=1:num
> for j=1:num
> for k in conflicts[i] 
> # if two tasks conflicts, they cannot be scheduled on the same task
> @constraint(m, x[i, j] + x[k, j] <= 1)
> end
> end
> end
>     
> for j=1:num # if processor j is assigned at least one task, then it is used
> @constraint(m, sum(x[i,j] for i=1:num) <= num*y[j])
> end
> 
> @objective(m, Min, sum(y[j] for j=1:num))
> JuMP.optimize!(m)
> 
> return (JuMP.value.(x), JuMP.value.(y), JuMP.objective_value(m))
> 
> ```
> 
> end  
> ;

Finally, it is run in

> (A2\_x, A2\_y, A2\_obj) = minNumProcessors(end\_time, start\_time, conflicts)  
> println("Minimum number of processors needed: ", A2\_obj)

It is taking a very long time to run. Most of the questions here that deal with slow execution, has to do with plots. Can someone point out why it is so slow in my case ?

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [April 13, 2020, 5:42am UTC](https://discourse.julialang.org/t/code-taking-too-long-to-run-need-some-advice/37485/2 "2020-04-13T05:42:35Z")

</div>

I am on mobile now, but there is a page in the documentation with all the “tricks” to avoid performance issues.

In yr case one for sure: avoid global variables or make them const.

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [April 13, 2020, 5:45am UTC](https://discourse.julialang.org/t/code-taking-too-long-to-run-need-some-advice/37485/3 "2020-04-13T05:45:04Z")

</div>

[https://docs.julialang.org/en/v1/manual/performance-tips/](https://docs.julialang.org/en/v1/manual/performance-tips/)
