• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Performance optimization

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Performance optimization

    Hi,

    I just want to make sure that my code is optimize for best performance.

    I am painting lots of numbers and letters to the chart window through a loop and these objects are using different brushes.

    I notice that sometimes it slows performance when I move the chart with my mouse.

    Here is the structure of my code
    I have all objects to be painted in a SortedDictionary and it is part of the drawing method which is called from OnRender :

    foreach(KeyValuePair<int, List<string> k in dic)
    {
    List<string> list = k.Value as List<string>();

    for(int i=0; i<list.Count; i++)
    {

    curElement = list[i];
    // do stuff

    if(xxxx)
    brushColor = Brushes.Yellow
    else
    brushColor = Brushes.Red;

    brushColorDx = brushColor.ToDxBrush(RenderTarget);

    RenderTargert.DrawText(xxxxxxxxx, brushColorDx);

    }

    }

    And OnRenderTargetChanged I have :

    if(brushColorDx != null)
    brushColorDx.Dispose();


    I am just wondering if I manage the brushDx the right way for performance optimization.

    It seems that more and more painting (shrinking the chart as much as I can ) )slows down the indicator a bit

    #2
    Hello blar58,

    May I confirm that you are reusing both sharpdx brushes and media brushes and that the sharpdx brushes are being created in OnRendrTargetChanged from the media brush?

    http://ninjatrader.com/support/helpG...getchanged.htm

    http://ninjatrader.com/support/forum...249#post461249
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi Chelsea

      Yes I am reusing sharpdx and media brushes

      No brushdx are created on a method that is being called from OnRender.

      I have no choice because I know only the brushes when I loop through the dictionary which is being called from OnRender.

      The brushdx is disposed from OnRenderTargetChanged though.

      Comment


        #4
        Hello blar58,

        I would recommend creating/recreating the sharpdx brushes in OnRenderTargetChanged() so that these do not collect in memory.

        This can be done with a loop over a dictionary in OnRenderTargetChanged().
        Attached is an example that demonstrates.

        (edit, I've made some small changes to the script, but none that change what I am demonstrating)

        (second edit, since I'm posting how to do this with a dictionary here, I'm adding the example I've made without using a dictionary as well for convenience and to compare with.)
        Attached Files
        Last edited by NinjaTrader_ChelseaB; 06-07-2017, 07:26 AM.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Thank you Chelsea

          I think I will solve my perfromance problem.

          Quick question :
          Looking at the code you sent me what do these two lines from OnRender means :

          previousAntialiasMode = RenderTarget.AntialiasMode;

          RenderTarget.AntialiasMode = previousAntialiasMode;


          Should I use that in my code

          Thanks

          Comment


            #6
            Hello blar58,

            If you are changing the AntialiasMode, then yes, you should change this back.

            If you choose not to change the AntialiasMode, then no, no need to track the original mode and change it back.

            With the AntialiasMode set to Alias, the rendering is jagged, with AntialiasMode set to PerPrimitive the rendering is smooth.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Hello NT Community,

              Here's another example with brushes that demonstrates how to add a set of brushes to a expandable / collapsible class so these are grouped together in the properties window.

              (edited Nov 30, 2017 to correct a public variable that should have been a private variable which causes issues with saving an indicator preset)
              Attached Files
              Last edited by NinjaTrader_ChelseaB; 11-30-2017, 12:01 PM.
              Chelsea B.NinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by giogio1, 11-10-2019, 11:23 PM
              4 responses
              14 views
              0 likes
              Last Post bltdavid  
              Started by Dolosumo, Today, 01:05 AM
              0 responses
              1 view
              0 likes
              Last Post Dolosumo  
              Started by StevenNelson, Yesterday, 07:31 PM
              1 response
              9 views
              0 likes
              Last Post NinjaTrader_ChrisSch  
              Started by dtl-saw, Yesterday, 09:37 PM
              0 responses
              2 views
              0 likes
              Last Post dtl-saw
              by dtl-saw
               
              Started by TazoTodua, Yesterday, 03:01 PM
              3 responses
              13 views
              0 likes
              Last Post TazoTodua  
              Working...
              X