Math.abs() is using a lot of memory?


(Adam Edney) #1
length_X = Math.abs(object_list[i].x - returnObjects[x].x);
length_Y = Math.abs(object_list[i].y - returnObjects[x].y);

For some reason this is using around 20mb per seconds of memory,(this is used many time per a frame) But I don’t know why? Any idea?


(Martí Angelats i Ribera) #2

I don’t think that Math.abs() is that memory consumable. Maybe using big loops or using other thing can cause it.
Try to cange the Math.abs() for a number and test it again. Then you’ll see if that’s the problem.


(Ultima2876) #3

Avoid Math functions where possible. They probably aren’t as memory intensive as it seems here (20mb per second is a LOT! That sounds like a bitmap leak to me, unless this is in a ridiculous loop) but they are a lot slower than using a ternary operator:

(object_list[i].x - returnObjects[x].x) > 0 ? (object_list[i].x - returnObjects[x].x) : -(object_list[i].x - returnObjects[x].x);

This goes for Math.min and Math.max as well.


(TaylorAnderson) #4

Seems like the tradeoff to using something like that is code readability (though I’ve never used a ternary operator, so maybe they become much easier to read at a glance when you know how they work)


(Ultima2876) #5

You can pretty easily wrap it in your own little helper function :slight_smile:


(Jacob Albano) #6

Ternary operators are really cool.

var result:String = value ? "result if value is true" : "result if value is false";

(Zachary Lewis) #7

You can also chain 'em!

trace("myInt is", myInt != 0 ? myInt % 2 == 0 ? "even." : "odd." : "zero.");

(Jacob Albano) #8

Yeah, but only if you’re a lunatic coding wizard.


(Zachary Lewis) #9


(Ultima2876) #10

A lunatic coding wizard who has no regard for the minds of others. I think the general consensus is to avoid it, and if you HAVE to do it, turn the inner ternaries into functions.


(Jacob Albano) #11

I’m alright with them if they have parentheses around each expression, but I try to avoid them myself.


(Martí Angelats i Ribera) #12

I think that the ternary operators just need some practice. They are not that difficult.

Actually if you think it like and if and else (and of ourse else if) it’s not that hard to understand (and becouse is a one line operator allows you to use it as, for example, a function argument. .

Here the same example with if and else. As you can see they use a lot more space (both of them can be coded in one single line).

var result:String;
if(value)
{
    result = "result if value is true";
}
else
{
    result = "result if value is false";
}
var result:String;
if(myInt != 0)
{
    if(myInt % 2 == 0)
    {
        result = "even.";
    }
    else
    {
        result = "odd.";
    }
}
else
{
    result = "zero.";
}
trace("myInt is ", result);

(Ultima2876) #13

In that kind of case it’s almost always obvious what it’s doing. The kinds of ternary operations to avoid are things like this:

H = (C == 0 ? null : V == r ? (g - b) / C : V == g ? (b - r) / C + 2 : (r - g) / C + 4);

Yes, that is actual code from a project in an actual production environment.


(Martí Angelats i Ribera) #14

You can write in another way for the most complex ones (it’s more visual and easier to understand):

H = (
    c == 0
        ? null
        : V == r
            ? (g - b) / C
            : (r - g) / C + 4
);

It’s also true that there is almost no difference between using the ternary operator or the if-else sentence. In this kind of big things i would recomend to use if-else sentence.

The ternary operator was made for that small and obvious cases (it’s faster to write the code and easy to understand).


(Ultima2876) #15

Another gotcha about them is that some languages (most famously PHP) do the associativity in a different order for ternaries. So if you move amongst languages a lot, it can cause some weird bugs.


(Adam Edney) #16

Thanks for all the replys, i found that rectangle intersects is the best solution as its much faster, speaking about faster methods, which is the best way to draw a line as I know DRAW function is only really for debugging but I cannot get MovieClip to work, it doesn’t render for some reason, any idea why?


(Ultima2876) #17

If you’re just using FlashPunk and not drawing many lines, the utilities Draw class will be much faster than AS3’s native line drawing (MovieClip).


(Adam Edney) #18

But I will most likely be drawing quite a few lines, and i want to add filters to them to make them look like lasers. Any suggestion?


A more efficient method of creating a laser
(Ultima2876) #19

I’d use an Image instead. Make a small slice of your ‘laser’; say, 4x8 pixels, then scale it as appropriate (lineLength / 8) and rotate it. This’ll require some maths but will be the most efficient way to draw lines as it’s just drawing sprites.


(Mike Evmm) #20

Probably draw a sprite, apply X filter, extract its bitmapdata, draw image. I keep getting beaten to it.
(Note that @Ultima2876’s solution is much more efficient in terms of performance, but you’ll have to apply the “effect” on the pre-done image)