- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我只是 c++ 的初学者,所以我可能做错了什么,但无论如何我创建了一个 c++ dll 并从我的 wpf 项目中调用它:
C++代码:
extern "C" __declspec (dllexport) double writeTxt()
{
ofstream mf("c:\\cpp.txt");
for(int i=0;i<999;i++)
{
mf<<"xLine: \n";
}
mf.close();
return 1;
}
从 C# 调用代码:
[DllImport(@"C:\Users\neo\Documents\visual studio 2010\Projects\TestDll\Debug\TestDll.dll",
CallingConvention = CallingConvention.Cdecl)]
public static extern double writeTxt();
现在我正在尝试将执行时间与此 c# 函数进行比较:
double writeTxtCs()
{
StreamWriter sw = new StreamWriter(@"c:\cs.txt");
for (int i = 0; i < 999; i++)
{
sw.WriteLine("Line: " + i);
}
sw.Close();
return 0;
}
但是c#函数比c++函数快两倍左右。
像这样测试:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
long[] arr = new long[100];
Stopwatch sw = new Stopwatch();
for (int i = 0; i < 99; i++)
{
sw.Start();
//double xxx = writeTxt();
double xxx = writeTxtCs();
arr[i] = sw.ElapsedMilliseconds;
sw.Reset();
}
MessageBox.Show(arr.Average().ToString());
Close();
}
运行 C# 函数时,我通常需要 ~0.65 毫秒,而运行 C++ 函数时,我通常需要 ~1.1 毫秒。
我的问题是:我是不是做错了什么,或者在这种情况下,c# 真的比 c++ 更快吗?
最佳答案
所有其他答案都有有效点。除了那些:
您正在针对 C++ DLL 的“调试”版本进行测试,这可能会降低 C++ 性能,而不是它对 C# 性能的影响。尝试对两者进行优化,看看效果如何。
尽管如此,I/O 与“语言”并没有太大关系。更多的是关于运行时和操作系统。
关于C# StringWriter 比 C++ ofstream 更快(通过 pinvoke)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11109710/
我正在使用一个 StringWriter,我将它传递给一个方法以在 foreach 循环中写入值。我相信这会导致产生两个警告: CA2000 : Microsoft.Reliability : In
StringWriter 和 StringBuilder 之间有什么区别?何时应该使用其中之一? 最佳答案 我认为现有的任何答案都不能真正回答这个问题。两个类之间的实际关系是the adapter p
我正在使用 JAXB,我需要知道生成的 XML 的长度,所以我将内容编码到 StringWriter,所以哪种方式更好地获取长度: aStringWriter.getBuffer().length()
我用过 StringWriter writer = new StringWriter(); request.setCharacterEncoding("ISO-8859-1"); response.s
我需要创建一个 TextWriter 来填充一些数据。我将 StringWriter 用作 TextWriter,业务逻辑运行良好。 我有一个无法更改的技术要求,因为它可能会破坏所有客户端。我需要 T
我意识到在 java 中 GC 最终会清理对象,但我问的是不关闭你的字符串编写器是否是不好的做法,目前我正在这样做: private static String processTemplate(fi
我正在使用 .NET Core 3.1 有没有办法编辑StringWriter输出的xml节点? 例如,我想更改“utf-8”的大小写。当前的输出如下所示。 我的代码如下。 public class
我正在使用 .net 并且需要获取一些 html 文本,所以我想我会一起使用 HtmlTextWriter 和 StringWriter 来获取格式良好的 html。但是,尽管我编写代码的方式各不相同
我必须在我的项目中处理一个大字符串,它是一个简单页面的 html。 为此,我有两种可能性:使用 String 类或使用 StringWriter。我想知道性能和内存使用方面最好的是什么。 有什么区别以
在我的 StringWriter 上调用 ToString 时出现 OutOfMemoryException: StringWriter stringWriter = new System.IO.St
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 8 年前。 Improve this qu
我最近读到在StringWriter 和StringReader 中用于写入和读取StringBuilder。 当我使用 StringBuilder 对象时,它看起来是一个自给自足的类。 我们有各种读
我在 Web 应用程序的 Handler 类中有一个静态 StringWriter 变量,由该类中的多个私有(private)方法使用。每个方法都会将一个字符串附加到该变量,最后 StringWrit
我目前正在寻找一种简单的方法来序列化对象(在 C# 3 中)。 我用谷歌搜索了一些例子,并得出了类似的结果: MemoryStream memoryStream = new MemoryStream
好吧..这太奇怪了。我正在尝试在 android 中发出 http 请求。它有效,我可以使用 StringWriter “构建”一个字符串,但代码在 writer.toString() 处抛出异常。使
我有一个类数组,我要将其传递给 XML,以便稍后将其发送给 SQL Server 存储过程。问题是,当我执行 StringWriter 时,它会添加一些出现在最终 XML 中的转义字符 (=\)。 我
我有一个 StringWriter 变量 sw,它由 FreeMarker 模板填充。填充 sw 后,如何将其打印到文本文件? 我有一个 for 循环如下: for(2 times) { te
我想清除 XmlTextWriter 和 StringWriter 中的所有内容。 Flush() 没有成功。 XmlDocument doc = new XmlDocument();StringWr
我最近在尝试为我的 ruby session 重定向 ScriptEngine 输出时遇到了一个奇怪的问题。似乎无论我将什么传递到我的 ScriptEngine,它总是打印到 stdout 和 s
StringWriter 有一个 flush功能。刷新字符串缓冲区是什么意思? 最佳答案 它必须有一个 flush() 的实现,因为在它的父类(super class) Writer 中这是一个抽象方
我是一名优秀的程序员,十分优秀!