首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >插件互操作-访问速度比windows应用程序快15-20倍。

插件互操作-访问速度比windows应用程序快15-20倍。
EN

Stack Overflow用户
提问于 2018-06-04 14:13:54
回答 1查看 62关注 0票数 1

我不知道,StackOverflow是否适合询问性能问题,但我还没有找到更好的社区来解决这个问题。

基本上,我们有两个示例程序,一个是加载项,另一个是引用单词interop的winforms程序。

两者都实现了名为GetTabsFromParagraph的相同方法。

代码语言:javascript
复制
public class SlowExample
{
    public static void GetTabsFromParagraph(Paragraph para, Style style, List<Tabulator> tabList, bool getTabsForCase = false)
    {
        foreach (TabStop tab in para.TabStops)
        {
            if (tab.CustomTab)
            {
                bool showTab = true;
                foreach (TabStop ts in style.ParagraphFormat.TabStops)
                {
                    if (Math.Abs(ts.Position - tab.Position) < 0.001 &&
                        ts.Alignment == tab.Alignment)
                    {
                        showTab = false;
                        break;
                    }
                }
                if (showTab || getTabsForCase)
                {
                    Tabulator t = new Tabulator
                    {
                        Tabulatorausrichtung = 
                            tab.Alignment == WdTabAlignment.wdAlignTabLeft
                                ? TabulatorAusrichtung.Links 
                                : TabulatorAusrichtung.Rechts,
                        Tabulatorart = TabulatorArt.Tabulator,
                        Position = tab.Position
                    };

                    tabList.Add(t);
                }
            }
        }
        if (!getTabsForCase)
        {
            foreach (TabStop ts in style.ParagraphFormat.TabStops)
            {
                if (ts.CustomTab)
                {
                    bool showTab = true;
                    foreach (TabStop tab in para.TabStops)
                    {
                        if (Math.Abs(tab.Position - ts.Position) > 0.0001 || tab.Alignment != ts.Alignment)
                        {
                            showTab = false;
                            break;
                        }
                    }
                    if (showTab)
                    {
                        Tabulator t = new Tabulator
                        {
                            Tabulatorausrichtung = TabulatorAusrichtung.Geloescht,
                            Tabulatorart = TabulatorArt.Tabulator,
                            Position = ts.Position
                        };
                        tabList.Add(t);
                    }
                }
            }
        }
        if (Math.Abs(para.LeftIndent - style.ParagraphFormat.LeftIndent) > 0.001 || getTabsForCase)
        {
            Tabulator t = new Tabulator
            {
                Tabulatorausrichtung = TabulatorAusrichtung.Links,
                Tabulatorart = TabulatorArt.Einzug,
                Position = para.LeftIndent
            };
            tabList.Add(t);
        }
        if (Math.Abs(para.RightIndent - style.ParagraphFormat.RightIndent) > 0.001 || getTabsForCase)
        {
            Tabulator t = new Tabulator
            {
                Tabulatorausrichtung = TabulatorAusrichtung.Rechts,
                Tabulatorart = TabulatorArt.Einzug,
                Position = para.RightIndent
            };
            tabList.Add(t);
        }
        if (Math.Abs(para.FirstLineIndent - style.ParagraphFormat.FirstLineIndent) > 0.001 || getTabsForCase)
        {
            Tabulator t = new Tabulator
            {
                Tabulatorausrichtung = TabulatorAusrichtung.ErstzeilenEinzug,
                Tabulatorart = TabulatorArt.Einzug,
                Position = para.FirstLineIndent
            };
            tabList.Add(t);
        }
    }

    public class Tabulator
    {
        private TabulatorArt m_Tabulatorart;
        private TabulatorAusrichtung m_Tabulatorausrichtung;
        private float m_Position;
        private bool m_UebernahmeInFolgedokument = false;

        public float Position
        {
            get { return m_Position; }
            set { m_Position = value; }
        }

        public float PositionOrg
        {
            get;
            set;
        }

        public float PositionInCm
        {
            get
            {
                return (m_Position / 28.35F);
            }
            set
            {
                m_Position = value * 28.35F;
            }
        }

        public TabulatorArt Tabulatorart
        {
            get { return m_Tabulatorart; }
            set { m_Tabulatorart = value; }
        }

        public TabulatorAusrichtung Tabulatorausrichtung
        {
            get { return m_Tabulatorausrichtung; }
            set { m_Tabulatorausrichtung = value; }
        }

        public TabulatorAusrichtung TabulatorausrichtungOrg
        {
            get;
            set;
        }


        public bool UebernahmeInFolgedokument
        {
            get { return m_UebernahmeInFolgedokument; }
            set { m_UebernahmeInFolgedokument = value; }
        }
    }

    public enum TabulatorArt
    {
        Invalid = 0,
        Tabulator = 1,
        Einzug = 2
    }

    public enum TabulatorAusrichtung
    {
        Invalid = 0,
        Links = 1,
        Rechts = 2,
        ErstzeilenEinzug = 3,
        Geloescht = 4,
    }
}

在这两个程序中,我都加载了一个应用程序,打开一个带有几个段落和选项卡的文档,并为每个段落运行以下方法:

代码语言:javascript
复制
    private void TestSlowMethod(Word.Document document)
    {
        Word.Paragraphs documentParagraphs = document.Paragraphs;
        List<Tabulator> tabList = new List<Tabulator>();
        long swElapsedMilliseconds = 0;
        foreach (Word.Paragraph documentParagraph in documentParagraphs)
        {
            Word.Style style = documentParagraph.get_Style();

            Stopwatch sw = new Stopwatch();
            sw.Start();

            SlowExample.GetTabsFromParagraph(documentParagraph, style, tabList, true);

            sw.Stop();
            swElapsedMilliseconds += sw.ElapsedMilliseconds;
            Debug.WriteLine(sw.ElapsedMilliseconds + "\r\n");
        }

        MessageBox.Show("Total ms: " + swElapsedMilliseconds);
        Debug.WriteLine("Done...");
    }

我发现加载项运行的速度快了10-20倍。

  • 地址:每次通话20-30 ms
  • WinForms工具:每次呼叫200到300 ms

为什么会这样呢?我的假设是,加载项运行在与单词应用程序相同的上下文/进程中。但这就是真正的原因吗?

我们能修好吗?我们的软件从外接程序转移到WPF +互操作字。

提前感谢!

EN

回答 1

Stack Overflow用户

发布于 2018-06-04 14:45:02

一些性能修正:

请在这里更早地检查getTabsForCase

代码语言:javascript
复制
    if (tab.CustomTab)
    {
        bool showTab = true;

        if (getTabsForCase) //insert this here, no need to run if getTabsForCase.
            foreach (TabStop ts in style.ParagraphFormat.TabStops)
            {
                if (Math.Abs(ts.Position - tab.Position) < 0.001 &&
                    ts.Alignment == tab.Alignment)
                {
                    showTab = false;
                    break;
                }
            }
        if (showTab || getTabsForCase)
        {
            Tabulator t = new Tabulator
            {
                Tabulatorausrichtung = 
                    tab.Alignment == WdTabAlignment.wdAlignTabLeft
                        ? TabulatorAusrichtung.Links 
                        : TabulatorAusrichtung.Rechts,
                Tabulatorart = TabulatorArt.Tabulator,
                Position = tab.Position
            };

            tabList.Add(t);
        }
    }

同样,在getTabsForCase语句中的所有计算之前对if进行检查:

代码语言:javascript
复制
//see getTabsForCase goes first
if (getTabsForCase || Math.Abs(para.LeftIndent - style.ParagraphFormat.LeftIndent) > 0.001)

首先将所有这些条件都修正为getTabsForCase --然后语句的其余部分就不需要计算了。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50682623

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档