1 Star 0 Fork 0

微距离/pythonnet

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
PythonCallingNetBenchmark.cs 1.47 KB
一键复制 编辑 原始数据 按行查看 历史
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using BenchmarkDotNet.Attributes;
using Python.Runtime;
namespace Python.PerformanceTests
{
[Config(typeof(BaselineComparisonConfig))]
public class PythonCallingNetBenchmark: BaselineComparisonBenchmarkBase
{
[Benchmark]
public void ReadInt64Property()
{
using (Py.GIL())
{
var locals = new PyDict();
locals.SetItem("a", new NetObject().ToPython());
Exec($@"
s = 0
for i in range(50000):
s += a.{nameof(NetObject.LongProperty)}
", locals: locals);
}
}
[Benchmark]
public void WriteInt64Property() {
using (Py.GIL()) {
var locals = new PyDict();
locals.SetItem("a", new NetObject().ToPython());
Exec($@"
s = 0
for i in range(50000):
a.{nameof(NetObject.LongProperty)} += i
", locals: locals);
}
}
static void Exec(string code, PyDict locals)
{
MethodInfo exec = typeof(PythonEngine).GetMethod(nameof(PythonEngine.Exec));
object localsArg = typeof(PyObject).Assembly.GetName().Version.Major >= 3
? locals : locals.Handle;
exec.Invoke(null, new[]
{
code, localsArg, null
});
}
}
class NetObject
{
public long LongProperty { get; set; } = 42;
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/MicroDistanceStudio/pythonnet.git
git@gitee.com:MicroDistanceStudio/pythonnet.git
MicroDistanceStudio
pythonnet
pythonnet
master

搜索帮助