1. 程式人生 > >delphi 2007 vs E語言 vs C#執行速度

delphi 2007 vs E語言 vs C#執行速度

這是用E語言編譯的,因為我用的4.05試用版本,沒有辦法生成EXE檔案,只能在E語言環境 下執行

這是E語言的程式碼:

.版本 2

.程式集 視窗程式集1

.子程式 _按鈕1_被單擊
.區域性變數 時間1, 長整數型
.區域性變數 時間2, 長整數型
.區域性變數 結果, 整數型
.區域性變數 到文字, 整數型

時間1 = 取啟動時間 ()
結果 = SumTimes (到數值 (編輯框1.內容))
時間2 = 取啟動時間 () - 時間1

編輯框2.內容 = 編輯框2.內容 + 到文字 (結果) + “次累加運算耗費:” + 到文字 (時間2) + “毫秒” + #換行符


.子程式 SumTimes, 長整數型
.引數 timer, 長整數型
.區域性變數 結果, 長整數型


結果 = 0
.計次迴圈首 (timer, )
    結果 = 結果 + 1
.計次迴圈尾 ()
返回 (結果)

這是delphi 2007編譯的EXE檔案,可以看出差好多了

這是delphi的程式碼:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    btnAdd: TButton;
    Label1: TLabel;
    Memo1: TMemo;
    procedure btnAddClick(Sender: TObject);
  private
    { Private declarations }
    function SumTimes(i:Integer):string;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btnAddClick(Sender: TObject);
var
  dEnd:real;
  dStart:Integer;
  str:string;
begin
  dStart:=GetTickCount;
  str:=SumTimes(strToInt(trim(Edit1.Text)));
  dEnd:=GetTickCount-dStart;
  memo1.Lines.Add(edit1.Text+'次累加運算耗費:'+floattostr(dEnd)+' 毫秒');
end;

function TForm1.SumTimes(i: Integer):string;
var j,m:integer;

begin
  for j := 0 to i - 1 do
  begin
    m:=m+1;
  end;
  result:=inttostr(m);
end;

end.

這是我用C# 2005的測試程式,大家可以測試下。

下面是C#的程式碼:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace cSharpTest
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //System.DateTime currentTime = new System.DateTime();
            int iStart, iEnd;
            long st;
            iStart = Environment.TickCount;
            st = SumTimer(long.Parse(textBox1.Text));
            //System.DateTime currentTime1 = new System.DateTime();

            iEnd = Environment.TickCount - iStart;

            rtb.AppendText(textBox1.Text + "次累加運算耗費:" + iEnd.ToString()+" 毫秒/n");
           
        }
        private long SumTimer(long s)
        {
            long m=0;
            for (long i = 1; i <= s; i++)
            {
                m += 1;
            }
           
            return m;
        }
    }
}

結論:這幾個編譯器,可以看出效率最高的是直接編譯成機器碼的delphi,他所編譯出的程式碼大小(418K)。但是C#和E語言都不是直接編譯成機器碼,所以沒有什麼可比性,但是E語言的效率比C#高確實有點驚喜!

測試檔案: