??xml version="1.0" encoding="utf-8" standalone="yes"?>
Z克服array必须事先宣告大小的不便,STL?NET都有解决的方式,在STL的container当中Q速度最快的是std::vectorQ?NET当中是ArrayListQ所以我x试在同样的程序中Q若使用unmanaged 的std::vector是否会比managed ArrayList快?br />
/**/
/*
(C) OOMusou 2006
http://oomusou.cnblogs.com
Filename : VectorVsArrayList.cpp
Compiler : Visual C++ 8.0
Description : Demo std::vector and ArrayList in C++/CLI
*/
#include
"
stdafx.h
"
#include
<
vector
>
#include
<
ctime
>
#include
<
iostream
>
using
namespace
System;
using
namespace
System::Collections;
int
main()
{
//
Use STL std::vector
clock_t t1
=
clock();
std::vector
<
int
>
ivec;
for
(
int
i
=
0
; i
!=
10000000
;
++
i)
{
ivec.push_back(i);
}
t1
=
clock()
-
t1;
std::cout
<<
"
std::vector :
"
<<
(
double
)t1
/
CLOCKS_PER_SEC
<<
"
sec
"
<<
std::endl;
//
Use .NET ArrayList
clock_t t2
=
clock();
ArrayList
^
al
=
gcnew ArrayList;
for
(
int
i
=
0
; i
!=
10000000
;
++
i)
{
al
->
Add(i);
}
t2
=
clock()
-
t2;
std::cout
<<
"
.NET ArrayList :
"
<<
(
double
)t2
/
CLOCKS_PER_SEC
<<
"
sec
"
<<
std::endl;
return
0
;
}
执行l果
std::vector :
5.618
sec
.NET ArrayList :
4.336
sec
請按L늹U? . .
执行l果令我讶异Q我原本预期unmanaged的std::vector应该会比较快Q但l果却是managed的ArrayList较快Q我觉得可能的原因是Qmanaged的数据放在CLR内,而unmanaged数据攑֜不同的地方,当unmanaged的std::vector要在managed的执行环境中昄Ӟq要l过marshaling的动作,所以浪费了一些时_cM?NET中用COM的dllӞ也必ȝqmarshalingQ所以速度较慢?br />
回到我第一个问题,用C++开发ASP.NET有什么好?在这个简单的E序里可以发玎ͼC++/CLI的最大特Ԍ让你可以managed和unmanagedE序写在一P同时使用.NET Framework和传lC++的libraryQ如STL、boostQ这是C#所做不到的Q假如你的工E领域已l有很多libraryQ是用C/C++写的Q可能是10几年前的东西Q目前根本没有C#版本的libraryQ而你又急需q些library的function开?NETE序QC++/CLI很方便了,让你同时拥抱C/C++ library?NET FrameworkQ这也是Microsoft开发C++/CLI的原因?br />
l论
在C++/CLI开发managed codeQ执行速度不会比C#、VB快,虽然C++/CLI可以开发unmanaged codeQ但?NET Framework有相对应的支持时Q应先考虑.NET FrameworkQ然后再考虑其它C++自己的libraryQ当然C++/CLI不见的适合所有开发h员,但对于一些开发h员是很方便的?br />




<%
@ Page AutoEventWireup="true" Inherits="HelloWorld" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
Using C++/CLI in ASP.NET<br />
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></div>
</form>
</body>
</html>


#using <system.dll>
#using <mscorlib.dll>
#using <system.web.dll>
using namespace System;
using namespace System::Web::UI::WebControls;

public ref class HelloWorld : public System::Web::UI::Page
{
protected:
Button^ Button1;
Label^ Label1;
public:
void Button1_Click(Object^ sender, EventArgs^ e)
{
this->Label1->Text = "Hello World";
return;
}
};
cl /clr HelloWorld.aspx.cpp /link /dll /out:HelloWorld.dll
最后只要将HelloWorld.aspx攑ֈc:\Inetpub\wwwroot\下,HelloWorld.dll攑ֈc:\Inetpub\wwwroot\bin\下,完成deployment?br />
l论
很多C++无法开发ASP.NETQANSI C++的确不能Q但C++/CLI则可以,事实上,M.NET下的语言都可以开发ASP.NETQ虽然Visual Studio 2005工具不见的支持,但只要透过一些小技巧,你依然可以用妛_Ƣ的.NET语言开发ASP.NET?br />
Reference
ASP.NET with Managed C++ , Soliant, The code project.