Snapman集合了TCC编译器可以直接编译执行C语言脚本,其脚本执行效率和C#编译程序进行效率对比,包括下面3方面:
1、函数执行效率
2、数字转换成字符串
3、字符串的叠加
这是C#代码:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace TestEfficiency { public partial class Form1 : Form { public Form1() { InitializeComponent(); } int fibonacci(int n) { if (n == 1 || n == 2) return 1; return fibonacci(n - 1) + fibonacci(n - 2); } string numberConvert() { string result = ""; for (int i = 0; i < 14000000; i++ ) { result = i.ToString(); } return result; } string stringAppend() { string conc = "StringBuilder sb = new StringBuilder(1024);"; StringBuilder sb = new StringBuilder(1024); for (int i = 0; i < 1400; i++) { sb = new StringBuilder(1024); for (int j = 0; j < 10000; j++ ) { sb.Append(conc); } } return sb.ToString(); } private void button1_Click(object sender, EventArgs e) { DateTime fbeforDT = System.DateTime.Now; fibonacci(45); DateTime fafterDT = System.DateTime.Now; TimeSpan ts = fafterDT.Subtract(fbeforDT); textBox1.Text = string.Format("fibonacci spend: {0}ms.", ts.TotalMilliseconds); DateTime nbeforDT = System.DateTime.Now; numberConvert(); DateTime nafterDT = System.DateTime.Now; TimeSpan nts = nafterDT.Subtract(nbeforDT); textBox2.Text = string.Format("numberConvert spend: {0}ms.", nts.TotalMilliseconds); DateTime sbeforDT = System.DateTime.Now; stringAppend(); DateTime safterDT = System.DateTime.Now; TimeSpan sts = safterDT.Subtract(sbeforDT); textBox3.Text = string.Format("stringAppend spend: {0}ms.", sts.TotalMilliseconds); } } }
这是C#编译出来Release的页面:
这是Snapman系统中写的TCC脚本代码:
int fibonacci(int n) { if(n==1||n==2) return 1; return fibonacci(n-1) + fibonacci(n-2); } char* numberConvert() { char* result = (char*)malloc(128); for (int i = 0; i < 14000000; i++ ) { itoa(i,result,10); } return result; } char* stringAppend() { char* conc = "StringBuilder sb = new StringBuilder(1024);"; char* sb = (char*)malloc(1024),*p; int len = 1024, cl = strlen(conc), curlen = 0; for (int i = 0; i < 1400; i++) { free(sb); len = 1024; curlen = 0; sb = (char*)malloc(1024); for(int j = 0; j < 10000; j++) { if(curlen + cl > len) { p = (char*)malloc(len*2); memcpy(p,sb,curlen); free(sb); sb = p; len *= 2; } memcpy(sb+curlen,conc,cl); curlen += cl; } } sb[curlen] = 0; return sb; } void main() { int starTicc = GetTickCount(); fibonacci(45); wPrint(L"fibonacci spend: %dms",GetTickCount() - starTicc); starTicc = GetTickCount(); free(numberConvert()); wPrint(L"numberConvert spend: %dms",GetTickCount() - starTicc); starTicc = GetTickCount(); free(stringAppend()); wPrint(L"stringAppend spend: %dms",GetTickCount() - starTicc); }
这是Snapman页面:
两种代码的设计和实现方法完全一样,其运行结果对比:
C#执行结果:
Snapman执行结果:
结论:对比结果发现C#除了最高效的StringBuilder和TCC字符串操作执行速度接近外其他的运行速度都要比Snapman的运行速度慢一些,所以Snapman的脚本运行速度是非常快速的。
Snapman的主页:http://www.snapman.xyz
时间: 2024-10-08 08:32:31