# Math Operator Speed

**URL:** <https://forums.solar2d.com/t/math-operator-speed/287562>\
**Category:** General Questions/Discussion\
**Created:** [February 19, 2011, 9:15pm UTC](https://forums.solar2d.com/t/math-operator-speed/287562 "2011-02-19T21:15:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![StillLearning](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/s/46a35a/32.png) [@StillLearning](https://forums.solar2d.com/u/StillLearning)\
**Post date:** [February 19, 2011, 9:15pm UTC](https://forums.solar2d.com/t/math-operator-speed/287562/1 "2011-02-19T21:15:58Z")

</div>

According to the Corona Programming Guide at [http://bit.ly/hwL7nl](http://bit.ly/hwL7nl), multiplication yields faster results than division.  
Therefore, would…

```auto
fog.x \> 480 + fog.width / 2  
```

… be better written as…

```auto
fog.x \> 480 + fog.width \*0.5  
```

Thoughts? [import]uid: 12397 topic\_id: 6872 reply\_id: 306872[/import]

---

<div class="post-metadata">

**Author:** ![jmp909](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/j/919ad9/32.png) [@jmp909](https://forums.solar2d.com/u/jmp909)\
**Post date:** [February 19, 2011, 9:51pm UTC](https://forums.solar2d.com/t/math-operator-speed/287562/2 "2011-02-19T21:51:41Z")

</div>

yes, it specifically states that example!!  
  
Math: fast vs slow  
Multiplication x\*0.5 is faster than division x/2.

but also read this:  
[http://developer.anscamobile.com/forum/2010/11/13/performance-tip-setting-isvisiblefalse-slow-reading-display-object-xy-slow](http://developer.anscamobile.com/forum/2010/11/13/performance-tip-setting-isvisiblefalse-slow-reading-display-object-xy-slow)

if fog.width is constant it might be faster caching it as a local value, and reusing it in calculations

ie [lua]local fogHalfWidth = fog.width \* 0.5[/lua]

of course this only probably really matters if you’re doing that for lots of objects like in my example [import]uid: 6645 topic\_id: 6872 reply\_id: 23981[/import]

---

<div class="post-metadata">

**Author:** ![StillLearning](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/s/46a35a/32.png) [@StillLearning](https://forums.solar2d.com/u/StillLearning)\
**Post date:** [February 19, 2011, 10:35pm UTC](https://forums.solar2d.com/t/math-operator-speed/287562/3 "2011-02-19T22:35:53Z")

</div>

Great idea THX!

I do use many of these and they’re all constant widths… plus they sit in two different enterFrame functions called from two different buttons.

**Question:** Since they’re in an enterFrame, are they being recalculated each frame?

**Question:** Would I use your cached calculation like so…

```auto
fog.x \> 480 + fogHalfWidth  
```

**Question:** Should the local pre-calculation variable go inside of the functions containing the calculations?

Sorry if these are dumb questions… as the name says, I’m StillLearning.

Thanks again.

[import]uid: 12397 topic\_id: 6872 reply\_id: 23993[/import]

---

<div class="post-metadata">

**Author:** ![jmp909](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/j/919ad9/32.png) [@jmp909](https://forums.solar2d.com/u/jmp909)\
**Post date:** [February 20, 2011, 8:10am UTC](https://forums.solar2d.com/t/math-operator-speed/287562/4 "2011-02-20T08:10:07Z")

</div>

if fog width is constant throughout your app then you could just put the definition right at the top of your file. (if you know the value before you’ve even loaded the fog for instance). again this is quite extreme optimisation though…does you game actually need it? is it dropping framerate?

[import]uid: 6645 topic\_id: 6872 reply\_id: 24041[/import]
