Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #30655

Re: Impact of implicit arrays...

Newsgroups comp.lang.javascript
Date 2016-06-13 22:00 -0700
References <njhj52$hl3$1@gioia.aioe.org>
Message-ID <4a1c28ea-447b-4e9b-9c75-68ef85c379e7@googlegroups.com> (permalink)
Subject Re: Impact of implicit arrays...
From "Michael Haufe (TNO)" <tno@thenewobjective.com>

Show all headers | View raw


On Saturday, June 11, 2016 at 12:52:43 PM UTC-5, Chris M. Thomasson wrote:
> I created a little complex number library for some new 
> fractal encryption techniques I am going to put online. 
> I am a bit worried about all of the arrays I am using in 
> the following code:
> ______________________________ 
> "use strict";
> 
> function ct_complex_add(c0, c1)
> {
>     return [c0[0] + c1[0], c0[1] + c1[1]];
> }
> 
> function ct_complex_sub(c0, c1) {
>     return [c0[0] - c1[0], c0[1] - c1[1]];
> }
> 
> function ct_complex_abs(c)
> {
>     return Math.sqrt(c[0] * c[0] + c[1] * c[1]);
> }
> 
> function ct_complex_mul(c0, c1)
> {
>     return [c0[0] * c1[0] - c0[1] * c1[1],
>             c0[0] * c1[1] + c0[1] * c1[0]];
> }
> 
> function ct_complex_mul_real(c0, c)
> {
>     return [c0[0] * c, c0[1] * c];
> }
> 
> function ct_complex_pow(c, p)
> {
>     var l = ct_complex_abs(c);
>     var s = Math.pow(l, p);
>     var a = Math.atan2(c[1], c[0]) * p;
> 
>     return [Math.cos(a) * s, Math.sin(a) * s];
> }
> 
> function ct_complex_roots(c, p)
> {
>     var l = ct_complex_abs(c);
>     var s = Math.pow(l, 1.0 / p);
>     var a = Math.atan2(c[1], c[0]) / p;
> 
>     var n = Math.ceil(Math.abs(p));
>     var as = (Math.PI * 2) / p;
>     var roots = [];
> 
>     for (var i = 0; i < n; ++i)
>     {
>         roots.push([Math.cos(a + as * i) * s,
>                     Math.sin(a + as * i) * s]);
>     }
> 
>     return roots;
> }
> ______________________________ 
> 
> 
> If I call any of the functions above in a large 
> iteration, will memory start to explode and stress 
> out the garbage collector? I mean will there be 
> millions of arrays that are allocated during heavy 
> load? This is purely functional implementation. Can 
> an intrusive technique give better performance? 
> 
> Thank you.


Potentially, yes. 

You want to avoid the intermediate data structures obviously, but the question is how? Sadly since JavaScript uses CBV evaluation semantics, you won't get any help there [1]. Which means it's up to you to do some creative thinking in your data structures.

Looking at what you have thus far, I don't think there is much you can do to improve things at this level of discourse, at best I think you might get a constant factor speedup at the significant expense of readability. (Bit fiddling and Math.foo() alternatives...)

I think what I would do is evaluate how the application is developing and at that stage try to discover some form of higher level rules that you can exploit. For example, for manipulating sums:

1 + 2 + 3 + ... + n = (n*(n+1)) / 2

Another thought is to switch to polar coordinates instead of Cartesian coordinates as it would simplify multiplication and division at least. With fractal based work this might be more intuitive anyway, yes?




[1] <https://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_value>

Back to comp.lang.javascript | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Impact of implicit arrays... "Chris M. Thomasson" <nospam@nospam.com> - 2016-06-11 10:52 -0700
  Re: Impact of implicit arrays... Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-06-12 12:16 +0200
    Re: Impact of implicit arrays... "Chris M. Thomasson" <nospam@nospam.com> - 2016-06-13 12:23 -0700
  Re: Impact of implicit arrays... "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-06-13 22:00 -0700
    Re: Impact of implicit arrays... "Chris M. Thomasson" <nospam@nospam.com> - 2016-06-14 17:17 -0700
      Re: Impact of implicit arrays... Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-06-15 06:55 +0200
      Re: Impact of implicit arrays... "Chris M. Thomasson" <nospam@nospam.invalid> - 2016-06-14 22:19 -0700
      Re: Impact of implicit arrays... "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-06-18 12:47 -0700
        Re: Impact of implicit arrays... "Chris M. Thomasson" <invalid@invalid.invalid> - 2016-06-21 14:43 -0700
          Re: Impact of implicit arrays... "Christoph M. Becker" <cmbecker69@arcor.de> - 2016-06-22 00:10 +0200
            Re: Impact of implicit arrays... "Chris M. Thomasson" <invalid@invalid.invalid> - 2016-06-21 15:28 -0700
              Re: Impact of implicit arrays... "Chris M. Thomasson" <invalid@invalid.invalid> - 2016-06-21 15:31 -0700
                Re: Impact of implicit arrays... "Christoph M. Becker" <cmbecker69@arcor.de> - 2016-06-22 11:55 +0200
                Re: Impact of implicit arrays... "Chris M. Thomasson" <invalid@invalid.invalid> - 2016-06-23 12:23 -0700
                Re: Impact of implicit arrays... "Chris M. Thomasson" <invalid@invalid.invalid> - 2016-06-23 14:14 -0700
                Re: Impact of implicit arrays... "Chris M. Thomasson" <invalid@invalid.invalid> - 2016-06-23 14:16 -0700
                Re: Impact of implicit arrays... "Chris M. Thomasson" <invalid@invalid.invalid> - 2016-06-23 18:14 -0700
                Re: Impact of implicit arrays... "Chris M. Thomasson" <invalid@invalid.invalid> - 2016-06-23 18:46 -0700
          Re: Impact of implicit arrays... "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-06-21 17:35 -0700
            Re: Impact of implicit arrays... "Chris M. Thomasson" <invalid@invalid.invalid> - 2016-06-21 21:08 -0700
              Re: Impact of implicit arrays... "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-06-21 23:09 -0700
                Re: Impact of implicit arrays... "Chris M. Thomasson" <invalid@invalid.invalid> - 2016-06-23 14:38 -0700
                Re: Impact of implicit arrays... "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-06-24 07:16 -0700
                Re: Impact of implicit arrays... "Chris M. Thomasson" <invalid@invalid.invalid> - 2016-06-25 18:03 -0700
                Re: Impact of implicit arrays... "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-06-25 18:24 -0700
                Re: Impact of implicit arrays... "Chris M. Thomasson" <invalid@invalid.invalid> - 2016-07-01 13:52 -0700

csiph-web