代码拉取完成,页面将自动刷新
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;
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。