[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

Optimise adder

JT

5/21/2015 2:19:00 PM

Feel free try to make it faster but still keep it general "anybase". It assumes input formatted in base. And arrOne>=arrTwo. Addresult holds the result of the integer adds.

<script language="Javascript">

/* ADD TWO THE TWO VARIABLES STORED IN ADDONE AND ADDTWO USING CHOSEN BASE */

function naiveAddB(base, addOne, addTwo)
{
abigArr = addOne.length;
asmallArr = addTwo.length;
addResult = [];
remainder = 0;
for (i = 0; i < abigArr; i ++ )
{
if (isNaN(addTwo[i])) addTwo[i] = 0;
addResult[i] = addOne[i] + addTwo[i] + remainder;
if (addResult[i] >= base)
{
addResult[i] = addResult[i] - base;
remainder = 1;
}
else
{
remainder = 0;
}
}

// If strings of equal length but there is a remainder;
if (remainder == 1)
{
addResult[i ++ ] = 1;
}
else
{
addResult.splice(i, 1);
}
return addResult;
}

base=10;
var arrOne = [3,8,7,6,5,4,3,2,1,0,9,8,7,6,5,4,3,2,1,0,9,8,7,6,5,4,3,2,1,0,9,8,7,6,5,4,3,2,1,0,9,8,7,6,5,4,3,2,1,0,9,8,7,6,5,4,3,2,1,0,9,8,7,6,5,4,3,2,1,0,9,8,7,6,5,4,3,2,1,0,9,8,7,6,5,4,3,2,1,0,9,8,7,6,5,4,3,2,1,0,9,8,7,6,5,4,3,2,1,0,9,8,7,6,5,4,3,2,1,0,9,8,7,6,5,4,3,2,1,0,9,8,7,6,5,4,3,2,1,0,9,8,7,6,5,4,3,2,1,0,9,8,7,6,5,4,3,2,1,0,9,8,7,6,5,4,3,2,1,0,9,8,7,6,5,4,3,2,1,0,9,8,7,6,5,4,3,2,1,0,9,8,7,6,5,4,3,2,1,0,9,8,7,6,5,4,3,2,1,0,9,8,7,6,5,4,3,2,1,0,9,8,7,6,5,4,3,2,1,0,9,8,7,6,5,4,3,2,1,0,9,8,7,6,5,4,3,2,1,0,9,8,7,6,5,4,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8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0];

/* TIMER START */
var start = new Date().getTime();
for(j=1;j<10000;j++) {
addResult=naiveAddB(base,arrOne,arrTwo);
}
var end = new Date().getTime();
var time = end - start;
document.write(addResult,"<BR>"," Timing = ", time, " ms");

</script>

40 Answers

Ben Bacarisse

5/21/2015 3:02:00 PM

0

jonas.thornvall@gmail.com writes:

> Feel free try to make it faster

Optimising bad code is not an appealing project.

The correct plan is to re-write it as a proper function and, what's
more, make it a function that operates on some big number object type.

> but still keep it general "anybase".

It does not seem to permit any base. You claimed to be allowing
arbitrary precision numbers as the base, but the code assumes that the
base is an ECMAScript Number value.

<snip>
--
Ben.

JT

5/21/2015 3:43:00 PM

0

Den torsdag 21 maj 2015 kl. 17:01:39 UTC+2 skrev Ben Bacarisse:
> jonas.thornvall@gmail.com writes:
>
> > Feel free try to make it faster
>
> Optimising bad code is not an appealing project.
>
> The correct plan is to re-write it as a proper function and, what's
> more, make it a function that operates on some big number object type.
>
> > but still keep it general "anybase".
>
> It does not seem to permit any base. You claimed to be allowing
> arbitrary precision numbers as the base, but the code assumes that the
> base is an ECMAScript Number value.
>
> <snip>
> --
> Ben.
Well you would have to format the imput to that base. But the function allow anybase upto integer size of JavaScript which of course mean that it allow add upto sqrt integer size if equal size operands. Or integer size for operand one and zero for two.

If you want format it to Another base you can use this.
http://jt.node365.se/arithme...

JT

5/21/2015 3:55:00 PM

0

Den torsdag 21 maj 2015 kl. 17:01:39 UTC+2 skrev Ben Bacarisse:
> jonas.thornvall@gmail.com writes:
>
> > Feel free try to make it faster
>
> Optimising bad code is not an appealing project.
>
> The correct plan is to re-write it as a proper function and, what's
> more, make it a function that operates on some big number object type.
>
> > but still keep it general "anybase".
>
> It does not seem to permit any base. You claimed to be allowing
> arbitrary precision numbers as the base, but the code assumes that the
> base is an ECMAScript Number value.
>
> <snip>
> --
> Ben.
Actually it is infinite precision arithmetic, i have no idea what you mean you simply feed it two times if you want decimals, and carry over a 1 if to the second add if needed. There is no extra penalty in time for that. The decimals Points is handled by the parser.

It permit anybase, but you have to format your number into that base Before feeding the function, try base two.

Ben Bacarisse

5/21/2015 4:04:00 PM

0

jonas.thornvall@gmail.com writes:

> Den torsdag 21 maj 2015 kl. 17:01:39 UTC+2 skrev Ben Bacarisse:
>> jonas.thornvall@gmail.com writes:
>>
>> > Feel free try to make it faster
>>
>> Optimising bad code is not an appealing project.
>>
>> The correct plan is to re-write it as a proper function and, what's
>> more, make it a function that operates on some big number object type.
>>
>> > but still keep it general "anybase".
>>
>> It does not seem to permit any base. You claimed to be allowing
>> arbitrary precision numbers as the base, but the code assumes that the
>> base is an ECMAScript Number value.
>>
>> <snip>
>> --
>> Ben.

Is there any chance that I can persuade you to follow Usenet conventions
and (1) snip signatures; (2) reply in-line to the points being made?

> Actually it is infinite precision arithmetic, i have no idea what you
> mean you simply feed it two times if you want decimals, and carry over
> a 1 if to the second add if needed. There is no extra penalty in time
> for that. The decimals Points is handled by the parser.
>
> It permit anybase, but you have to format your number into that base
> Before feeding the function, try base two.

The code you posted does not permit any base.

The discussion would be more productive if you addressed the points made
rather than quoting the full message and the adding something that you
feel is relevant at the end. I explain exactly why your code does not
permit any base and exactly why I though that was a key part of your
project. You did not address either of these points.

--
Ben.

JT

5/21/2015 4:06:00 PM

0

Den torsdag 21 maj 2015 kl. 17:54:42 UTC+2 skrev jonas.t...@gmail.com:
> Den torsdag 21 maj 2015 kl. 17:01:39 UTC+2 skrev Ben Bacarisse:
> > jonas.thornvall@gmail.com writes:
> >
> > > Feel free try to make it faster
> >
> > Optimising bad code is not an appealing project.
> >
> > The correct plan is to re-write it as a proper function and, what's
> > more, make it a function that operates on some big number object type.
> >
> > > but still keep it general "anybase".
> >
> > It does not seem to permit any base. You claimed to be allowing
> > arbitrary precision numbers as the base, but the code assumes that the
> > base is an ECMAScript Number value.
> >
> > <snip>
> > --
> > Ben.
> Actually it is infinite precision arithmetic, i have no idea what you mean you simply feed it two times if you want decimals, and carry over a 1 if to the second add if needed. There is no extra penalty in time for that. The decimals Points is handled by the parser.
>
> It permit anybase, but you have to format your number into that base Before feeding the function, try base two.

It is a linear time adder and the higher the base the faster the add.

JT

5/21/2015 4:13:00 PM

0

Den torsdag 21 maj 2015 kl. 18:04:22 UTC+2 skrev Ben Bacarisse:
> jonas.thornvall@gmail.com writes:
>
> > Den torsdag 21 maj 2015 kl. 17:01:39 UTC+2 skrev Ben Bacarisse:
> >> jonas.thornvall@gmail.com writes:
> >>
> >> > Feel free try to make it faster
> >>
> >> Optimising bad code is not an appealing project.
> >>
> >> The correct plan is to re-write it as a proper function and, what's
> >> more, make it a function that operates on some big number object type.
> >>
> >> > but still keep it general "anybase".
> >>
> >> It does not seem to permit any base. You claimed to be allowing
> >> arbitrary precision numbers as the base, but the code assumes that the
> >> base is an ECMAScript Number value.
> >>
> >> <snip>
> >> --
> >> Ben.
>
> Is there any chance that I can persuade you to follow Usenet conventions
> and (1) snip signatures; (2) reply in-line to the points being made?
>
> > Actually it is infinite precision arithmetic, i have no idea what you
> > mean you simply feed it two times if you want decimals, and carry over
> > a 1 if to the second add if needed. There is no extra penalty in time
> > for that. The decimals Points is handled by the parser.
> >
> > It permit anybase, but you have to format your number into that base
> > Before feeding the function, try base two.
>
> The code you posted does not permit any base.

Not true, here i changed base same code, different number format.

It does add in linear time in anybase.

<script language="Javascript">

/* ADD TWO THE TWO VARIABLES STORED IN ADDONE AND ADDTWO USING CHOSEN BASE */

function naiveAddB(base, addOne, addTwo)
{
abigArr = addOne.length;
asmallArr = addTwo.length;
addResult = [];
remainder = 0;
for (i = 0; i < abigArr; i ++ )
{
if (isNaN(addTwo[i])) addTwo[i] = 0;
addResult[i] = addOne[i] + addTwo[i] + remainder;
if (addResult[i] >= base)
{
addResult[i] = addResult[i] - base;
remainder = 1;
}
else
{
remainder = 0;
}
}

// If strings of equal length but there is a remainder;
if (remainder == 1)
{
addResult[i ++ ] = 1;
}
else
{
addResult.splice(i, 1);
}
return addResult;
}

base=2;
var arrOne = [0,1,1,1,0,1];
var arrTwo = [1,1,1,1,1,1]
/* TIMER START */
var start = new Date().getTime();
for(j=1;j<10000;j++) {
addResult=naiveAddB(base,arrOne,arrTwo);
}
var end = new Date().getTime();
var time = end - start;
document.write(addResult,"<BR>"," Timing = ", time, " ms");

</script>





> The discussion would be more productive if you addressed the points made
> rather than quoting the full message and the adding something that you
> feel is relevant at the end. I explain exactly why your code does not
> permit any base and exactly why I though that was a key part of your
> project. You did not address either of these points.
>
> --
> Ben.

Ben Bacarisse

5/21/2015 7:02:00 PM

0

jonas.thornvall@gmail.com writes:

> Den torsdag 21 maj 2015 kl. 18:04:22 UTC+2 skrev Ben Bacarisse:
>> jonas.thornvall@gmail.com writes:
>>
>> > Den torsdag 21 maj 2015 kl. 17:01:39 UTC+2 skrev Ben Bacarisse:
>> >> jonas.thornvall@gmail.com writes:
>> >>
>> >> > Feel free try to make it faster
>> >>
>> >> Optimising bad code is not an appealing project.
>> >>
>> >> The correct plan is to re-write it as a proper function and, what's
>> >> more, make it a function that operates on some big number object type.
>> >>
>> >> > but still keep it general "anybase".
>> >>
>> >> It does not seem to permit any base. You claimed to be allowing
>> >> arbitrary precision numbers as the base, but the code assumes that the
>> >> base is an ECMAScript Number value.
>> >>
>> >> <snip>
>> >> --
>> >> Ben.
>>
>> Is there any chance that I can persuade you to follow Usenet conventions
>> and (1) snip signatures; (2) reply in-line to the points being made?
>>
>> > Actually it is infinite precision arithmetic, i have no idea what you
>> > mean you simply feed it two times if you want decimals, and carry over
>> > a 1 if to the second add if needed. There is no extra penalty in time
>> > for that. The decimals Points is handled by the parser.
>> >
>> > It permit anybase, but you have to format your number into that base
>> > Before feeding the function, try base two.
>>
>> The code you posted does not permit any base.
>
> Not true, here i changed base same code, different number format.

Yes, true. Here's a test case for you:

base=9007199254740992;
var arrOne = [base-1];
var arrTwo = [2];

<snip>
--
Ben.

Ben Bacarisse

5/21/2015 7:03:00 PM

0

jonas.thornvall@gmail.com writes:

> Den torsdag 21 maj 2015 kl. 17:54:42 UTC+2 skrev jonas.t...@gmail.com:
>> Den torsdag 21 maj 2015 kl. 17:01:39 UTC+2 skrev Ben Bacarisse:
>> > jonas.thornvall@gmail.com writes:
>> >
>> > > Feel free try to make it faster
>> >
>> > Optimising bad code is not an appealing project.
>> >
>> > The correct plan is to re-write it as a proper function and, what's
>> > more, make it a function that operates on some big number object type.
>> >
>> > > but still keep it general "anybase".
>> >
>> > It does not seem to permit any base. You claimed to be allowing
>> > arbitrary precision numbers as the base, but the code assumes that the
>> > base is an ECMAScript Number value.
>> >
>> > <snip>
>> > --
>> > Ben.
>> Actually it is infinite precision arithmetic, i have no idea what
>> you mean you simply feed it two times if you want decimals, and
>> carry over a 1 if to the second add if needed. There is no extra
>> penalty in time for that. The decimals Points is handled by the
>> parser.
>>
>> It permit anybase, but you have to format your number into that base
>> Before feeding the function, try base two.
>
> It is a linear time adder and the higher the base the faster the add.

Up to the maximum base you permit but you don't permit any base so it's
not possible to test the main assertion you have made.

--
Ben.

JT

5/21/2015 7:39:00 PM

0

Den torsdag 21 maj 2015 kl. 21:03:40 UTC+2 skrev Ben Bacarisse:
> jonas.thornvall@gmail.com writes:
>
> > Den torsdag 21 maj 2015 kl. 18:04:22 UTC+2 skrev Ben Bacarisse:
> >> jonas.thornvall@gmail.com writes:
> >>
> >> > Den torsdag 21 maj 2015 kl. 17:01:39 UTC+2 skrev Ben Bacarisse:
> >> >> jonas.thornvall@gmail.com writes:
> >> >>
> >> >> > Feel free try to make it faster
> >> >>
> >> >> Optimising bad code is not an appealing project.
> >> >>
> >> >> The correct plan is to re-write it as a proper function and, what's
> >> >> more, make it a function that operates on some big number object type.
> >> >>
> >> >> > but still keep it general "anybase".
> >> >>
> >> >> It does not seem to permit any base. You claimed to be allowing
> >> >> arbitrary precision numbers as the base, but the code assumes that the
> >> >> base is an ECMAScript Number value.
> >> >>
> >> >> <snip>
> >> >> --
> >> >> Ben.
> >>
> >> Is there any chance that I can persuade you to follow Usenet conventions
> >> and (1) snip signatures; (2) reply in-line to the points being made?
> >>
> >> > Actually it is infinite precision arithmetic, i have no idea what you
> >> > mean you simply feed it two times if you want decimals, and carry over
> >> > a 1 if to the second add if needed. There is no extra penalty in time
> >> > for that. The decimals Points is handled by the parser.
> >> >
> >> > It permit anybase, but you have to format your number into that base
> >> > Before feeding the function, try base two.
> >>
> >> The code you posted does not permit any base.
> >
> > Not true, here i changed base same code, different number format.
>
> Yes, true. Here's a test case for you:
>
> base=9007199254740992;
> var arrOne = [base-1];
> var arrTwo = [2];
>
> <snip>
> --
> Ben.

You are oncfused Ben or trolling i know you understand that if the array is reversed also any output more than one digit has to be reversed.

See example below. **WORK JUST FINE***
base=900719925474091;

var arrOne = [900719925474092];
var arrTwo = [99999999999999];

100000000000000,1 The array is reversed and so are the big number... It is correct.
Timing = 4 ms


<script language="Javascript">

/* ADD TWO THE TWO VARIABLES STORED IN ADDONE AND ADDTWO USING CHOSEN BASE */

function naiveAddB(base, addOne, addTwo)
{
abigArr = addOne.length;
asmallArr = addTwo.length;
addResult = [];
remainder = 0;
for (i = 0; i < abigArr; i ++ )
{
if (isNaN(addTwo[i])) addTwo[i] = 0;
addResult[i] = addOne[i] + addTwo[i] + remainder;
if (addResult[i] >= base)
{
addResult[i] = addResult[i] - base;
remainder = 1;
}
else
{
remainder = 0;
}
}

// If strings of equal length but there is a remainder;
if (remainder == 1)
{
addResult[i ++ ] = 1;
}
else
{
addResult.splice(i, 1);
}
return addResult;
}

base=900719925474091;

var arrOne = [900719925474092];
var arrTwo = [99999999999999];


//base=2;
//var arrOne = [0,1,1,1,0,1];
//var arrTwo = [1,1,1,1,1,1]
/* TIMER START */
var start = new Date().getTime();
for(j=1;j<10000;j++) {
addResult=naiveAddB(base,arrOne,arrTwo);
}
var end = new Date().getTime();
var time = end - start;
document.write(addResult,"<BR>"," Timing = ", time, " ms");

</script>

JT

5/21/2015 7:52:00 PM

0

Den torsdag 21 maj 2015 kl. 21:03:40 UTC+2 skrev Ben Bacarisse:
> jonas.thornvall@gmail.com writes:
>
> > Den torsdag 21 maj 2015 kl. 18:04:22 UTC+2 skrev Ben Bacarisse:
> >> jonas.thornvall@gmail.com writes:
> >>
> >> > Den torsdag 21 maj 2015 kl. 17:01:39 UTC+2 skrev Ben Bacarisse:
> >> >> jonas.thornvall@gmail.com writes:
> >> >>
> >> >> > Feel free try to make it faster
> >> >>
> >> >> Optimising bad code is not an appealing project.
> >> >>
> >> >> The correct plan is to re-write it as a proper function and, what's
> >> >> more, make it a function that operates on some big number object type.
> >> >>
> >> >> > but still keep it general "anybase".
> >> >>
> >> >> It does not seem to permit any base. You claimed to be allowing
> >> >> arbitrary precision numbers as the base, but the code assumes that the
> >> >> base is an ECMAScript Number value.
> >> >>
> >> >> <snip>
> >> >> --
> >> >> Ben.
> >>
> >> Is there any chance that I can persuade you to follow Usenet conventions
> >> and (1) snip signatures; (2) reply in-line to the points being made?
> >>
> >> > Actually it is infinite precision arithmetic, i have no idea what you
> >> > mean you simply feed it two times if you want decimals, and carry over
> >> > a 1 if to the second add if needed. There is no extra penalty in time
> >> > for that. The decimals Points is handled by the parser.
> >> >
> >> > It permit anybase, but you have to format your number into that base
> >> > Before feeding the function, try base two.
> >>
> >> The code you posted does not permit any base.
> >
> > Not true, here i changed base same code, different number format.
>
> Yes, true. Here's a test case for you:
>
> base=9007199254740992;
> var arrOne = [base-1];
> var arrTwo = [2];

You are very confused why do you think it is wrong?
1,1
Timing = 4 ms

Think again ..........90+2 would be 1,0 comes out like this.

0,1
Timing = 4 ms

base=900719925474092;
var arrOne = [900719925474090];
var arrTwo = [2];