Giter Site home page Giter Site logo

PY 整数如何执行除法 about phpy HOT 6 CLOSED

Dmcz avatar Dmcz commented on August 16, 2024
PY 整数如何执行除法

from phpy.

Comments (6)

Dmcz avatar Dmcz commented on August 16, 2024

关于效率,我将numpy与bcmath 做了个简单的对比

  • 做乘法,bcmath快于调用numpy
// 示例代码
$min = 1;
$max = 100000000;

$arr = [];
for ($i=0; $i < 100000; $i++) { 
    $arr[] =  $min + mt_rand() / mt_getrandmax() * ($max - $min);
}    

$np = PyCore::import("numpy");

function mul(float $a, float $b): float{
    return (float) bcmul((string)$a, (string)$b, 10);
}

var_dump(microtime(true));
foreach($arr as $index => $item){
    $a = mul($item, $index);
}
var_dump(microtime(true));
foreach($arr as $index => $item){
    $a = $np->multiply($item, $index);
}
var_dump(microtime(true));

// 输出
// float(1715827413.047093)
// float(1715827413.135797)
// float(1715827413.299653)
  • 数组计算, numpy 更优
// 示例代码
$np = PyCore::import("numpy");

var_dump(microtime(true));

$arr = range(0, 99999);

var_dump(microtime(true));

$list = $np->arange(0, 100000, 1);

var_dump(microtime(true));

$arr = array_map(function($n) {
    return (float) bcmul((string) $n, '21855.12345236', 10);
}, $arr);

var_dump(microtime(true));

$np->multiply($list, 21855.12345236);

var_dump(microtime(true));

// 输出
float(1715827680.609606)
float(1715827680.610147)
float(1715827680.610507)
float(1715827680.663684)
float(1715827680.664193)

from phpy.

matyhtf avatar matyhtf commented on August 16, 2024

python 和 phpy 的操作是完全等价的,这个方法 python 代码一样不能运行

python
Python 3.11.5 (main, Sep 11 2023, 13:54:46) [GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> a = 123
>>> a.__div__(2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'int' object has no attribute '__div__'. Did you mean: '__dir__'?

正确的写法是:

PyCore::int(2)->__truediv__(2); 

或者

PyCore::int(2)->__floordiv__(2); 

执行结果:

php -r "var_dump(PyCore::int(2)->__truediv__(2));"
float(1)

from phpy.

matyhtf avatar matyhtf commented on August 16, 2024

关于链式写法,我们考虑下,看看如何支持

PyCore::int(2)->__add__(1)->__mul__(2); 

from phpy.

Dmcz avatar Dmcz commented on August 16, 2024

ok. 可能我那天拼写错了,我试过__floordiv__不行, 抱歉。

from phpy.

matyhtf avatar matyhtf commented on August 16, 2024

已支持将 数字作为对象返回

PyCore::setOptions(['numeric_as_object' => true]);
$rs = PyCore::int(2)->__add__(3)->__mul__(7);

from phpy.

Dmcz avatar Dmcz commented on August 16, 2024

已支持将 数字作为对象返回

PyCore::setOptions(['numeric_as_object' => true]);
$rs = PyCore::int(2)->__add__(3)->__mul__(7);

ok,thx.

from phpy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.