I've been working on improving performance of BCMath lately, and I found that I can get the div and mod in one calculation. This is obviously faster than calculating it twice separately.
Do you think there's a demand for this feature?
e.g.
[$quot, $rem] = bcdivmod('123', '2');
// $quot is '61', $rem is '1'
The naming and return value scheme is inspired by Python and Ruby.
Of course, if this is added, equivalent functionality will be added to the Number class.
I've been working on improving performance of BCMath lately, and I found that I can get the div and mod in one calculation. This is obviously faster than calculating it twice separately.
Do you think there's a demand for this feature?
Since, when calculating either the div or the mod you basically get the other for free (as you've found), and they often go together in use (I have some number of things to distributed among some number of columns: I end up with *this many* things in each column and *this many* left over) so I can definitely see a convenience in this.
Otherwise, if you want both, you essentially end up doing the same computation twice:
On Tue, Jun 25, 2024, at 9:11 AM, Saki Takamachi wrote:
Hi internals,
I've been working on improving performance of BCMath lately, and I
found that I can get the div and mod in one calculation. This is
obviously faster than calculating it twice separately.
Do you think there's a demand for this feature?
e.g.
[$quot, $rem] = bcdivmod('123', '2');
// $quot is '61', $rem is '1'
The naming and return value scheme is inspired by Python and Ruby.
Of course, if this is added, equivalent functionality will be added to
the Number class.
Regards,
Saki
This isn't something I'm likely to ever use as I don't do complex math much, but it seems like a reasonable optimization for those that do. No objection.
My only question is the pseudo-tuple return, which is rarely used in PHP (although I've used it myself, I think exactly once), and I think this would be the first use of it in a built in library. (I may be wrong on that.) I don't have a particular alternative to suggest, just flagging that as the main part that could warrant discussion.
I’ve been working on improving performance of BCMath lately, and I found that I can get the div and mod in one calculation. This is obviously faster than calculating it twice separately.
Do you think there’s a demand for this feature?
e.g.
[$quot, $rem] = bcdivmod('123', '2');
// $quot is '61', $rem is '1'
The naming and return value scheme is inspired by Python and Ruby.
Of course, if this is added, equivalent functionality will be added to the Number class.
This isn’t something I’m likely to ever use as I don’t do complex math much, but it seems like a reasonable optimization for those that do. No objection.
My only question is the pseudo-tuple return, which is rarely used in PHP (although I’ve used it myself, I think exactly once), and I think this would be the first use of it in a built in library. (I may be wrong on that.) I don’t have a particular alternative to suggest, just flagging that as the main part that could warrant discussion.
–Larry Garfield
It’s actually already used by gmp_div_qr(), so +1 from me.
---- On Tue, 25 Jun 2024 10:11:10 +0100 Saki Takamachi wrote ---
> Hi internals,
>
> I've been working on improving performance of BCMath lately, and I found that I can get the div and mod in one calculation. This is obviously faster than calculating it twice separately.
>
> Do you think there's a demand for this feature?
>
> e.g.
> ```
> [$quot, $rem] = bcdivmod('123', '2');
> // $quot is '61', $rem is '1'
> ```
>
> The naming and return value scheme is inspired by Python and Ruby.
>
> Of course, if this is added, equivalent functionality will be added to the Number class.
I'm wondering whether this needs to be a change to the API, or if it might be better implemented as a purely internal optimisation to BCMath / Number - in some way memoize both the quotient and the remainder when either div or mod is called, and then document for users that calling one after the other will be extremely cheap to run.
But maybe that brings up too many questions about memory usage - e.g. is the memory for this reserved in advance for each instance of Number, or allocated when one of these functions is called, and should it be freed even while the object remains in scope to avoid using lots of memory if many numbers are divided or modded.