Comments (6)
关于效率,我将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.
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.
关于链式写法,我们考虑下,看看如何支持
PyCore::int(2)->__add__(1)->__mul__(2);
from phpy.
ok. 可能我那天拼写错了,我试过__floordiv__
不行, 抱歉。
from phpy.
已支持将 数字作为对象返回
PyCore::setOptions(['numeric_as_object' => true]);
$rs = PyCore::int(2)->__add__(3)->__mul__(7);
from phpy.
已支持将 数字作为对象返回
PyCore::setOptions(['numeric_as_object' => true]); $rs = PyCore::int(2)->__add__(3)->__mul__(7);
ok,thx.
from phpy.
Related Issues (20)
- docker内的liunx的python安装方式,能来个详细点的教程吗 HOT 3
- py2php转换错误例子
- 加载phpy扩展失败 HOT 1
- python类继承,PHP怎么实现 HOT 2
- phpy错误的把python的空字典转换成了php的空数组 HOT 2
- 导入第三方包的时候出现错误 HOT 2
- alpine下存在php模块加载错误 HOT 2
- 编译成功了 但是python import报错 HOT 1
- 这种情况是什么原因? HOT 1
- 看了说是可以支持 Linux Alpine 平台,那么有docker的Alpine,python镜像教程或者容器镜像吗? HOT 4
- Python.h: No such file or directory HOT 1
- 请问支持conda的虚拟环境吗? HOT 1
- PHP-FPM中如何使用? HOT 2
- 参照Dockerfile,使用的是Alpine镜像容器,php -m列表没有显示成功 HOT 6
- PyList能否直接转化成php的数组 HOT 1
- Dockerfile.alpine执行构建镜像失败 HOT 1
- 我自己构建的phpy.so加载报错
- Python 对象内存泄露问题 HOT 2
- mac m1芯片是否支持安装?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from phpy.