none
Efficiency of IronPython declined accompany by Execution increasing RRS feed

  • Question

  • Efficiency of IronPython declined accompany by Execution increasing

    Test Environment:

    .Net Framework 3.5

    IronPython 2.7.7

    Code has ran for ten million times。Average time spent per 1000 executions are illustrated on following chart:


    Across the result of test,Efficiency drop phenomenally fast after two million executions for each cycle, meanwhile,the memory leaking issue after this has been detected。 The following images reveal the memory usage after ten million executions。


    There is no problem mentioned above been found by using .Net Framework version 4.0 to Compile the same test code. The images below is the new result of Average time spent per 1000 executions,


    And the new condition of Memory Usage per ten millions executions


    It seems the problem in .Net Framework 4.0 has been fixed, can you explain me the details about how these problems been fixed and which part has been substituted, Thx a lot! ^_^

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Scripting.Hosting;
    using Microsoft.Scripting;
    using System.Threading;
    using IronPython.Hosting;
    using System.Reflection;
    using IronPython.Runtime;
    using System.IO;
    using System.Diagnostics;
    using System.Runtime.InteropServices;
    using System.Globalization;
    
    namespace IronPythonMemoryTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                if (File.Exists("log.csv"))
                {
                    File.Delete("log.csv");
                }
    
                TestExecute(10000000); 
    
                Console.Read();
            }
            private static void TestExecute(int times)
            {
                Stopwatch stopwatch = new Stopwatch();
                int count = 1;
                long ticks = 0;
                while (count<times)
                {
                    DataContextforTest data = new DataContextforTest();
                    ScriptRunner runner = new ScriptRunner();
                    data.Number = new Random().Next(5);
                    stopwatch.Start();
                    runner.SetVariable("_data", data);
                    runner.ExecuteDelegate<int>("_data.Number + 2");
                    runner.RemoveVariable("_data");
                    stopwatch.Stop();
                    ticks = ticks + stopwatch.ElapsedTicks;
                    if (count % 1000 == 0)
                    {
                        Write(count, ticks);
                        ticks = 0;
                    }
                    stopwatch.Reset();
                    count++;
                }
            }
    
            static void Write(int count,long ticks)
            {
                Console.WriteLine(string.Format("{0}/{1}", count, (ticks / 1000).ToString()));
            }
        }
    
        public class DataContextforTest
        {
            public int Number;
            private string _name;
            public string Name
            {
                get
                {
                    if (string.IsNullOrEmpty(this._name))
                    {
                        this._name = "Default";
                    }
                    return this._name;
                }
                set
                {
                    this._name = value;
                }
            }
        }
    
    
        class ScriptRunner
        {
            public static ScriptRuntime _scriptRuntime = Python.CreateRuntime();
            public static ScriptEngine _scriptEngine = _scriptRuntime.GetEngine("py");
            public ScriptScope _scriptScope;
            private static ScriptScope _staticScriptScope;
    
            public void SetStaticVariable(string variableName, object value)
            {
                if (_staticScriptScope == null)
                {
                    _staticScriptScope = _scriptEngine.CreateScope();
                }
    
                _staticScriptScope.SetVariable(variableName, value);
            }
            public void SetVariable(string variableName, object value)
            {
                if (_scriptScope == null)
                {
                    _scriptScope = _scriptEngine.CreateScope();
                }
    
                _scriptScope.SetVariable(variableName, value);
            }
    
            public void RemoveVariable(string variableName)
            {
                if (_scriptScope == null)
                {
                    _scriptScope = _scriptEngine.CreateScope();
                }
                _scriptScope.RemoveVariable(variableName);
            }
            public T ExecuteDelegate<T>(string lambdaExpression)
            {
                CompiledCode code = _scriptEngine.CreateScriptSourceFromString(lambdaExpression, SourceCodeKind.Expression).Compile();
    
                if (_scriptScope == null)
                {
                    _scriptScope = _scriptEngine.CreateScope();
                }
                return code.Execute<T>(_scriptScope);
            }
            public ScriptRunner()
            {
                _scriptScope = _scriptEngine.CreateScope();
            }
        }
    }
    Tuesday, August 13, 2019 1:23 AM

All replies

  • Hi GuQiang,

    Thank you for posting here.

    I noted that IronPython is a third-party product, unfortunately we don't provide support for third party products in the forums, so questions specifically related to using the third party API, and you'll need to post in their forums.

    The CLR Forum discuss and ask questions about .NET Framework Base Classes (BCL) such as Collections, I/O, Regigistry, Globalization, Reflection. Also discuss all the other Microsoft libraries that are built on or extend the .NET Framework, including Managed Extensibility Framework (MEF), Charting Controls, CardSpace, Windows Identity Foundation (WIF), Point of Sale (POS), Transactions.

    Thank you for your understanding.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, August 14, 2019 1:29 AM