From: emekadavid on
On May 2, 3:46 pm, Ry Nohryb <jo...(a)jorgechamorro.com> wrote:
> On May 2, 1:53 pm, Spamless <Spaml...(a)Nil.nil> wrote:
>
>
>
> > On 2010-05-01, Evertjan. <exjxw.hannivo...(a)interxnl.net> wrote:
>
> > > VK wrote on 01 mei 2010 in comp.lang.javascript:
>
> > >> Indeed I was getting nuts by trying to visualize that "5-dimensional
> > >> array of 72 elements" because I hate to use things I do not "see" even
> > >> if they work properly.
>
> > > Having a5dimensionalarray, which each array maximized at 1000 is easy,
> > > if you do not fill the array to much.
>
> > > Use an object [here 3 members are used]:
>
> > ><script type='text/javascript'>
>
> > > var my5dim = {
> > >  '000!235!148!999!400':'qwerty',
> > >  '074!088!002!000!874':'asd',
> > >  '000!088!148!999!401':'blah'};
>
> > I was writing a little recursive routine in Javascript (it's a simple
> > language and I have the spider monkey interpreter) needing what was
> > effectively a two-dimensionalarray and originally used an object
> > (keys: ""+i1+","+i2)
>
> > In use, parameters were 100 to 500 and ... well, it was OK, but a
> > bit slow so I implemented it as a onedimensionalarray with
> > _indexer (one-one map from whole number pairs to whole numbers)
> > and that sped it up *quite* dramatically. It seems that with thousands
> > of keys, Javascript is not too efficient in searching the key space
> > to get a value. Perhaps I should have tried perl!
>
> That may have been so in the past, but it seems that it's not so
> anymore in most current browsers: this snippet tests lookup of 1e6
> elements in an array vs lookup in an object, and the results in my Mac
> are:
>
> FF 3.6:
> array  lookup: 258 ms
> object lookup: 258 ms
> Chrome:
> array  lookup: 7 ms
> object lookup: 6 ms
> Opera:
> array  lookup: 459 ms
> object lookup: 424 ms
> Safari:
> array  lookup: 9 ms
> object lookup: 577 ms
>
> http://jorgechamorro.com/cljs/098/
>
> <script type="text/javascript">
> onload= function () {
>   //debugger;
>
>   function log (txt) {
>     document.body.appendChild(
>       document.createElement('p')).innerHTML= txt;
>   }
>
>   var timer= (function () {
>     var time;
>     return function timer (p, msg) {
>       switch (p) {
>         case "start":
>         return (time= +new Date());
>         break;
>         case "stop":
>         return log((msg || "")+ (new Date()- time)+ " ms");
>         break
>       }
>     }
>   })();
>   var array= [];
>   var object= {};
>   var kMax= 1e6;
>   var n= kMax;
>   var acumulador= 0;
>
>   while (n--) {
>     object[n]= array[n]= n%2 ? 1 : -1;
>   }
>
>   n= kMax;
>   timer('start');
>   while (n--) {
>     acumulador+= array[n];
>   }
>   timer('stop', "array  lookup: ");
>
>   n= kMax;
>   timer('start');
>   while (n--) {
>     acumulador+= object[n];
>   }
>   timer('stop', "object lookup: ");
>
> };
>
> </script>
> --
> Jorge.

/*
* it's not useful to program when you cannot see it. get the
representation first.
* how do you do that for five dimensions? have you considered
representing
* your data as a list or sequence?
* see: a 2x2 array is just a list of 4 elements. a 2x2x2 is a list of
8
elements
* at the maximum. you just have to visualise the mappings from
dimensions to list.
* let's take an example. we'll use a tree as a bridge from the array
to
list.
* a 2x2 array can become a 4 element array, at maximum, if you
visualize it as
* a binary tree such that the elaves gives you the elements.
* we can represent it this way.
* the root node can accept two elements, let them be arrays.
* each of the two arrays can accept two elements and the leaves are
primitives.
*/
var chNodes = new Array(2);
var chNodes1 = new Array(2); //
var rootNodes = new Array(chNodes, chNodes1); //insert unique array
references else courting confusion
var leaves = ['never', 'look', 'at', 'invisibles'];
var count = 0;
for( var i=0; i<rootNodes.length; i++){
for (var j=0; j<chNodes.length; j++){
rootNodes[i][j] = leaves[count];
count = count + 1;
}
}
for(var k =0; k<rootNodes.length; k++){
for(var l=0; l<chNodes.length; l++){
alert(rootNodes[k][l]);
}
}
/*
* now for a 3 dimension array, using just pairs. maximal leaves
should
be
* 2x2x2 = 8 leaves. why not replace with an 8 element list or array
* the root array are pairs of ancestor arrays while the ancestor
arrays
are pairs
* of child arrays and child arrays have primitives as elements
*/
var leaven = ['why', 'not', 'replace', 'using', 'an', 'eight',
'element', 'list?'];
var chArray1= new Array(2);
var chArray2 = new Array(2);
var chArray3 = new Array(2);
var chArray4 = new Array(2); //name uniquely for every depth of the
tree
var ancestArray1 = new Array(chArray1, chArray2);
var ancestArray2 = new Array(chArray3, chArray4);
rootNodes = new Array(ancestArray1, ancestArray2);
var count = 0;
for(var i=0; i<rootNodes.length; i++){
for(var j =0; j<ancestArray1.length; j++){//can use any. same no.
of
elements
for (var k =0; k<chArray1.length; k++){ //also same nu of
elements
rootNodes[i][j][k] = leaven[count];
count = count + 1;
}
}
}
for(var l=0; l<rootNodes.length; l++){
for(var m =0; m<ancestArray1.length; m++){//can use any. same no.
of
elements
for (var n =0; n<chArray1.length; n++){ //also same nu of
elements
alert(rootNodes[l][m][n]);
}
}
}
/*you should be creative and design a 5 dimension array like this and
see why you can not
*replace it using a list? using a list or array as your
representation,
you
*should be creative and see that you can visualise this better than
you
would
*any n dimension array.
*/
i think am just lazing with the computer
--
two things i learnt from life:
the hardest things are the simplest of all; they just take time
the simple things are not hard at all
so i just KISS it.