C# 插件互操作访问速度比windows应用程序快15-20倍
我不知道StackOverflow是否适合询问性能问题,但我还没有找到任何更好的社区来解决这个问题 基本上我们有两个示例程序,一个是addin,另一个是引用interop这个词的winforms程序 两者都实现了相同的方法,称为C# 插件互操作访问速度比windows应用程序快15-20倍,c#,performance,office-interop,word-addins,C#,Performance,Office Interop,Word Addins,我不知道StackOverflow是否适合询问性能问题,但我还没有找到任何更好的社区来解决这个问题 基本上我们有两个示例程序,一个是addin,另一个是引用interop这个词的winforms程序 两者都实现了相同的方法,称为gettabsfromparation: public class SlowExample { public static void GetTabsFromParagraph(Paragraph para, Style style, List<Tabulat
gettabsfromparation
:
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,
}
}
公共类慢速示例
{
公共静态void getAbsFrom段落(段落段落,样式样式,列表列表,bool getAbsForCase=false)
{
foreach(第TabStop段中的TabStop tab)
{
如果(制表符自定义制表符)
{
bool showTab=true;
foreach(style.ParagraphFormat.TabStops中的制表符)
{
如果(数学Abs(ts位置-制表位置)<0.001&&
ts.对齐==制表符对齐)
{
showTab=false;
打破
}
}
if(showTab | | getabsforcase)
{
制表器t=新制表器
{
制表符Austrichtung=
tab.Alignment==WdTabAlignment.wdAlignTabLeft
?TABLOTORAUSTRICHTUNG.链接
:tabletorausrichtung.Rechts,
制表器制表器,
位置=制表符位置
};
添加(t);
}
}
}
如果(!GetAbsForCase)
{
foreach(style.ParagraphFormat.TabStops中的制表符)
{
如果(ts.CustomTab)
{
bool showTab=true;
foreach(第TabStop段中的TabStop tab)
{
if(数学绝对值(tab.Position-ts.Position)>0.0001 | | tab.Alignment!=ts.Alignment)
{
showTab=false;
打破
}
}
如果(显示选项卡)
{
制表器t=新制表器
{
tablatorausrichtung=tablatorausrichtung.Geloescht,
制表器制表器,
位置=ts.位置
};
添加(t);
}
}
}
}
if(Math.Abs(para.LeftIndent-style.ParagraphFormat.LeftIndent)>0.001 | | getAbsforCase)
{
制表器t=新制表器
{
tabletorausrichtung=tabletorausrichtung.Links,
tabletorart=tabletorart.Einzug,
位置=段落左缩进
};
添加(t);
}
if(Math.Abs(para.righdent-style.ParagraphFormat.righdent)>0.001 | | getAbsforCase)
{
制表器t=新制表器
{
tabletorausrichtung=tabletorausrichtung.Rechts,
tabletorart=tabletorart.Einzug,
位置=段落右缩进
};
添加(t);
}
if(Math.Abs(para.FirstLineIndent-style.ParagraphFormat.FirstLineIndent)>0.001 | | getAbsforCase)
{
制表器t=新制表器
{
tabletorausrichtung=tabletorausrichtung.ErstzeilenEinzug,
tabletorart=tabletorart.Einzug,
位置=第一线缩进
};
添加(t);
}
}
公共类制表器
{
私人制表艺术m_制表艺术;
私人制表公司m_制表公司;
私人浮动m_位置;
private bool m_UebernahmeInFolgedokument=false;
公众浮动头寸
{
获取{返回m_位置;}
设置{m_Position=value;}
}
公共浮动机构
{
得到;
设置
}
公共浮动位置
{
得到
{
返回(m_位置/28.35F);
}
设置
{
m_位置=值*28.35F;
}
}
公共制表艺术制表艺术
{
获取{return m_tabletorart;}
集合{m_tabletorart=value;}
}
公共表格
{
获取{return m_tabletorausrichtung;}
设置{m_tabletorausrichtung=value;}
}
公共表格
{
得到;
设置
}
公共图书馆
{
获取{return m_ebernahmeinfolgedokument;}
集合{m_UebernahmeInFolgedokument=value;}
}
}
公共枚举表艺术
{
无效=0,
制表器=1,
Einzug=2
}
公共统计表
{
无效=0,
链接=1,
Rechts=2,
ErstzeilenEinzug=3,
Geloescht=4,
}
}
在这两个程序中的每一个程序中,我加载一个应用程序,打开一个包含几个段落和选项卡的文档,然后对每个段落运行以下方法:
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...");
}
private void TestSlowMethod(Word.Document文档)
{
Word.段落文件段落=文件段落;
列表列表=新列表();
长膨胀毫秒=0;
foreach(Word.段落文档段落中的段落文档段落)
{
Word.Style Style=documentparagration.get_Style();
秒表sw=新秒表()
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);
}
}
//see getTabsForCase goes first
if (getTabsForCase || Math.Abs(para.LeftIndent - style.ParagraphFormat.LeftIndent) > 0.001)