Mình đang sử dụng QT cho một số bài toán cần hiệu năng tuy nhiên những gì mình thu được là không thỏa mãn. QT build ra App có hiệu năng khá thấp.
Mình có 1 mảng unsigned char gồm 200.000.000 phần tử, mình sẽ tăng giá trị các phần tử lên 1 bằng 1 vòng for và thực hiện 50 lần. Thời gian thực hiện sẽ được in ra màn hình.
Với QT 5.5 Windows 32 + MINGW 4.9(C++) :
QElapsedTimer tmr;
unsigned char *arr = new unsigned char[200000000];
qint64 proctime=0;
int main() {
while(true) {
proctime=0;
for(int loop=0; loop<50; loop++) {
tmr.restart();
for(int i=0; i<200000000; i++)
arr[i]++;
proctime+=tmr.elapsed();
}
printf("%d\n",proctime);
}
return 0;
}
Trong CSharp (thuần C#) :
static Stopwatch st = new Stopwatch();
static byte[] arr = new byte[200000000];
static long proctime = 0;
static unsafe void Main(string[] args) {
fixed (byte* ptr = arr) {
while (true) {
proctime = 0;
for (int loop = 0; loop < 50; loop++) {
st.Restart();
for (int i = 0; i < 200000000; i++)
ptr[i]++;
proctime += st.ElapsedMilliseconds;
}
Console.WriteLine(proctime.ToString());
}
}
}
Và CSharp kết hợp C++: (CSharp gọi hàm xử lý trong 1 dll build bằng C++ MSVC)
static Stopwatch st = new Stopwatch();
static byte[] arr = new byte[200000000];
static long proctime = 0;
[DllImport("DLL.dll")]
extern static void ProcCess(IntPtr ptr, int num);
static unsafe void Main(string[] args) {
fixed (byte* ptr = arr) {
while (true) {
proctime = 0;
for (int loop = 0; loop < 50; loop++) {
st.Restart();
ProcCess((IntPtr)ptr, 200000000);
proctime += st.ElapsedMilliseconds;
}
Console.WriteLine(proctime.ToString());
}
}
}
Đây là code của DLL C++:
extern "C"
{
__declspec(dllexport) void __stdcall ProcCess(unsigned char *ptr,int num) {
for (int i = 0; i < num; i++)
ptr[i]++;
}
}
Phần cứng : Core i3 4160 3.6GHz Windows7
Chế độ test : SafeMode.
Chế độ build 3 App : 32bit, Release
Kết quả:
QT nhanh hơn thuần CSharp 1 chút (10/8).
CSharp+ dll C++ nhanh nhất. Gấp gần 3 lần CSharp và QT.
Vậy vấn đề app của QT chậm chạp ở đây là gì. Làm thế nào mình khắc phục được.
Xin cảm ơn !