# Contents of the function run well when timed alone, but not when they are in a loop inside the function

**URL:** https://discourse.julialang.org/t/contents-of-the-function-run-well-when-timed-alone-but-not-when-they-are-in-a-loop-inside-the-function/76886
**Category:** Performance
**Tags:** question, performance, arrays, function, sparse
**Created:** [February 22, 2022, 7:13am UTC](https://discourse.julialang.org/t/contents-of-the-function-run-well-when-timed-alone-but-not-when-they-are-in-a-loop-inside-the-function/76886 "2022-02-22T07:13:40Z")
**Posts on this page:** 1
**Showing post:** 3

<div class="post-metadata">

### Author: ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)
#### Post date: [February 22, 2022, 8:33am UTC](https://discourse.julialang.org/t/contents-of-the-function-run-well-when-timed-alone-but-not-when-they-are-in-a-loop-inside-the-function/76886/3 "2022-02-22T08:33:00Z")

</div>

Given your code I can only speculate that you are referencing (non constant) global variables, which is a well known [performance pitfall](https://docs.julialang.org/en/v1/manual/performance-tips) in Julia. Maybe it would be helpful to compile a complete [MWE](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757)?

---

_[View the full topic](https://discourse.julialang.org/t/contents-of-the-function-run-well-when-timed-alone-but-not-when-they-are-in-a-loop-inside-the-function/76886)._
