After some trying, it was found that it is difficult to achieve various ideas in a short time using only C language. However, if it is compiled into CPP, there are fewer restrictions, and one can mix various languages and create their own struct and methods. Now I have tested the performance of various commonly used basic types and also explored better usage methods. The test results can serve as reference data for solving various problems with suitable data types in the future. In the process, I also wrote a mixed C language type called myChar and automatically allocated memory through a buffer mechanism, with a length attribute to record the length. It not only considers performance but also adds various new functions continuously, including automatic memory release mechanism, so that the data processing process can meet the intended conception without wasting time on complicated problems.
Some types and methods, such as ordered map and Python str addition operation, are too slow to be tested together for too long without results, and are therefore not considered to be used.
In actual development, I tend to mix various languages as much as possible, reusing all tested code, first developing a version that can work correctly in the shortest possible time, and then improving it slowly during usage. t1.cpp:
#include "mylib.h"
using namespace std;
int testPerformance (int ts = 10000 * 1000){
// print_ulimit_a();
printf("Testing Performance for %ld Millions times:\n",ts/1000/1000);
clock_t start, finish;
double total_time;
map<int , double> omap;
unordered_map<int,double> umap;
unordered_map<string, string> kmap;
vector<int> vlist;
string strpp;
myChar mychar;
char *cst=(char*)malloc(ts*sizeof(cst));
vector<int> ls;
int *ls1=(int*)malloc(ts*sizeof(int));
string str;
char testC[]="123456";
start = clock();
for (int i = 0; i <ts; i++)
{
mychar.cat(testC,6);
}
finish = clock();
total_time = (double)(finish - start) / CLOCKS_PER_SEC;
printf("myChar cat : %lf secs,%lf M/sec\n", total_time, ts / total_time / 1000 / 1000);
charsView(mychar,10,32);
start = clock();
for (int i = 0; i <ts; i++)
{
char temps=mychar.chars[i];
}
finish = clock();
total_time = (double)(finish - start) / CLOCKS_PER_SEC;
printf("myChar get : %lf secs,%lf M/sec\n", total_time, ts / total_time / 1000 / 1000);
start = clock();
for (int i = 0; i <ts; i++)
{
strpp+=testC;
}
finish = clock();
total_time = (double)(finish - start) / CLOCKS_PER_SEC;
printf("C++ string add ;%lf secs,%lf M/sec\n", total_time, ts / total_time / 1000 / 1000);
start = clock();
for (int i = 0; i <ts; i++)
{
char temps=strpp.at(i);
}
finish = clock();
total_time = (double)(finish - start) / CLOCKS_PER_SEC;
printf("C++ string get ;%lf secs,%lf M/sec\n", total_time, ts / total_time / 1000 / 1000);
start = clock();
for (int i = 0; i <ts; i++)
{
vlist.push_back(i);
}
finish = clock();
total_time = (double)(finish - start) / CLOCKS_PER_SEC;
printf("STD::vector push back ;%lf secs,%lf M/sec\n", total_time, ts / total_time / 1000 / 1000);
start = clock();
for (int i = 0; i <ts; i++)
{
int temp=vlist[i];
}
finish = clock();
total_time = (double)(finish - start) / CLOCKS_PER_SEC;
printf("STD::vector get ;%lf secs,%lf M/sec\n", total_time, ts / total_time / 1000 / 1000);
//return 0;
start = clock();
for (int i = 0; i <ts; i++)
{
umap[i]=i;
}
finish = clock();
total_time = (double)(finish - start) / CLOCKS_PER_SEC;
printf("STD::unordered_map set :%lf secs,%lf M/sec\n", total_time, ts / total_time / 1000 / 1000);
start = clock();
for (int i = 0; i <ts; i++)
{
double temp=umap[i];
}
finish = clock();
total_time = (double)(finish - start) / CLOCKS_PER_SEC;
printf("STD::unordered_map get :%lf secs,%lf M/sec\n", total_time, ts / total_time / 1000 / 1000.0);
start = clock();
for (int i = 0; i <ts; i++)
{
kmap[to_string(i)]="s";
}
finish = clock();
total_time = (double)(finish - start) / CLOCKS_PER_SEC;
printf("STD::unordered_map string set :%lf secs,%lf M/sec\n", total_time, ts / total_time / 1000.0 / 1000);
start = clock();
for (int i = 0; i <ts; i++)
{
string temp1=kmap[to_string(i)];
}
finish = clock();
total_time = (double)(finish - start) / CLOCKS_PER_SEC;
printf("STD :: unordered_map string get : %lf secs,%lf M/sec\n", total_time, ts / total_time /1000.0 / 1000);
start = clock();
for (int i = 0; i <ts; i++)
{
ls1[i] = i;
//ls.push_back(i);
//str+="g";
}
finish = clock();
total_time = (double)(finish - start) / CLOCKS_PER_SEC;
printf("C int array set :%lf secs,%lf M/sec\n", total_time, ts / total_time / 1000 / 1000);
start = clock();
for (int i = 0; i <ts;i++)
{
int v = ls1[i];
//int v=ls[i];
//str+="g";
}
finish = clock();
total_time = (double)(finish - start) / CLOCKS_PER_SEC;
printf("C int array get :%lf secs,%lf M/sec,\n", total_time, ts / total_time / 1000 / 1000);
start = clock();
for (int i = 0; i <ts; i++)
{
cst[i]=i%256;
//ls.push_back(i);
//str+="g";
}
finish = clock();
total_time = (double)(finish - start) / CLOCKS_PER_SEC;
printf("C char array set : %lf secs,%lf M/sec\n", total_time, ts / total_time / 1000 / 1000);
start = clock();
for (int i = 0; i <ts;i++)
{
char v = cst[i];
//int v=ls[i];
//str+="g";
}
finish = clock();
total_time = (double)(finish - start) / CLOCKS_PER_SEC;
printf("C char array get :%lf secs,%lf M/sec,\n", total_time, ts / total_time / 1000 / 1000);
free(mychar.chars);
free(ls1);
ls1=NULL;
free(cst);
cst=NULL;
printf("Testing Python 3.1.1 ,run loop for 1,000,000 Tines:\n");
system("python test2.py");
return 0;
}
int main()
{
testPerformance();
double t0,t;
char url[]="http://localhost:8000/static/test.mp3";
char url1[]="http://localhost:8000/static/chat.js";
char url2[]="http://localhost:8000/static/in.mp4";
printf("Testing download file from localhost and write to File with myChar:\n");
t0=clock();
myChar uc=readURL(url2);
t=(clock()-t0)/CLOCKS_PER_SEC;
printf("Downloaed %s in %lf secs,%lf MB/sec\n",url2,t,uc.length/t/1000/1000);
charsView(uc,20,16);
t0=clock();
char path[]="o.mp4";
writeToFile(path,uc.chars,uc.length);
t=(clock()-t0)/CLOCKS_PER_SEC;
printf("Data written to File %s in %lf secs,%lf MB/sec\n",path,t,uc.length/t/1000/1000);
free(uc.chars);
return 0;
}
test2.py:
import time
from fileDict3 import FileDict
import numpy
def timIt(f,ts):
t0=time.time()
f(ts)
t=(time.time()-t0)
print("Run %s loop for %d times in %f secs, %f M/secs " %(f.__name__,ts,t,ts/t/1000/1000))
ts=10000*100
fileDict=FileDict(":memory:")
pythonDict=dict()
pythonList=list()
pythonStr=str()
numpyArray=numpy.empty(0)
def testFileDictSet(ts=ts):
for i in range(ts):
fileDict["%d"%i]="%d"%i
def testFileDictGet(ts=ts):
for i in range(ts):
trmp=fileDict["%d"%i]
def testSQLFileDict(ts=ts):
timIt(testFileDictSet,ts)
timIt(testFileDictGet,ts)
def testPythonDictSet(ts=ts):
for i in range(ts):
pythonDict["%d"%i]="%d"%i
def testPythonDictGet(ts=ts):
for i in range(ts):
trmp=pythonDict["%d"%i]
def testPythonDict(ts=ts):
timIt(testPythonDictSet,ts)
timIt(testPythonDictGet,ts)
def testPythonListAppend(ts=ts):
for i in range(ts):
pythonList.append(i)
def testPythonListGet(ts=ts):
for i in range(ts):
trmp=pythonList[i]
def testPythonList(ts=ts):
timIt(testPythonListAppend,ts)
timIt(testPythonListGet,ts)
def testPythonStrAdd(ts=ts):
global pythonStr
pythonStr="".join(["s" for x in range(ts)])
def testPythonStrGet(ts=ts):
global pythonStr
for i in range(ts):
trmp=pythonStr[i]
def testPythonStr(ts=ts):
timIt(testPythonStrAdd,ts)
timIt(testPythonStrGet,ts)
def testNumpyArrayPut(ts=ts):
global numpyArray
numpyArray=numpy.array(range(ts))
def testNumpyArrayGet(ts=ts):
global numpyArray
for i in range(ts):
temp=numpyArray[i]
def testNumpyArray(ts=ts):
timIt(testNumpyArrayPut,ts)
timIt(testNumpyArrayGet,ts)
testPythonStr()
testNumpyArray()
testPythonList()
testPythonDict()
testSQLFileDict()
Test results on Termux:
cat t1.sh
g++ -g -std=c++17 t1.cpp -o ~/bin/t1 -l stdc++ -l z -l ncurses -l jpeg -l m -l png -l sqlite3(fast) brian@localhost:/sdcard/Documents/Cxxdroibash t1.sh
(fast) brian@localhost:/sdcard/Documents/Cxxdroid$ ~/bin/t1
Testing Performance for 10 Millions times:
myChar cat : 0.109706 secs,91.152717 M/sec
Size :60.000000 M, 10 sample slices with witdth=32:
12345612345612345612345612345612 -...- 12345612345612345612345612345612 -...- 12345612345612345612345612345612 -...- 12345612345612345612345612345612 -...- 12345612345612345612345612345612 -...- 12345612345612345612345612345612 -...- 12345612345612345612345612345612 -...- 12345612345612345612345612345612 -...- 12345612345612345612345612345612 -...- 12345612345612345612345612345612 -...-
myChar get : 0.029256 secs,341.810227 M/sec
C++ string add ;0.148384 secs,67.392711 M/sec
C++ string get ;0.031740 secs,315.059861 M/sec
STD::vector push back ;0.145763 secs,68.604516 M/sec
STD::vector get ;0.029890 secs,334.560054 M/sec
STD::unordered_map set :3.308387 secs,3.022621 M/sec
STD::unordered_map get :0.980168 secs,10.202333 M/sec
STD::unordered_map string set :14.202366 secs,0.704108 M/sec
STD :: unordered_map string get : 8.273668 secs,1.208654 M/sec
C int array set :0.043568 secs,229.526258 M/sec
C int array get :0.031669 secs,315.766207 M/sec,
C char array set : 0.037155 secs,269.142780 M/sec
C char array get :0.031384 secs,318.633699 M/sec,
Testing Python 3.1.1 ,run loop for 1,000,000 Tines:
Run testPythonStrAdd loop for 1000000 times in 0.037662 secs, 26.551942 M/secs
Run testPythonStrGet loop for 1000000 times in 0.031914 secs, 31.334215 M/secs
Run testNumpyArrayPut loop for 1000000 times in 0.067665 secs, 14.778615 M/secs
Run testNumpyArrayGet loop for 1000000 times in 0.102062 secs, 9.797990 M/secs
Run testPythonListAppend loop for 1000000 times in 0.044594 secs, 22.424756 M/secs
Run testPythonListGet loop for 1000000 times in 0.028341 secs, 35.284205 M/secs
Run testPythonDictSet loop for 1000000 times in 0.748319 secs, 1.336329 M/secs
Run testPythonDictGet loop for 1000000 times in 0.579722 secs, 1.724966 M/secs
Run testFileDictSet loop for 1000000 times in 4.431511 secs, 0.225657 M/secs
Run testFileDictGet loop for 1000000 times in 4.278479 secs, 0.233728 M/secs
Testing download file from localhost and write to File with myChar:
Downloaed http://localhost:8000/static/in.mp4 in 2.554132 secs,71.670279 MB/sec
Size :183.055000 M, 20 sample slices with witdth=16:
Ʊm�̾���<�vb� -...- �ѡ���A���Ȋ� -...- �:���~ϳ��qɢ -...- D=�˘Qw��c�)�� -...- .n%"�8]%>�r�.� -...- ��K���[�(�� -...- ���65ű8왗~��0t -...- �߃���xXv�����T! -...- ��<k�^�@�_��I -...- wǭqq�p��5J#�� -...- ;;Ռ��)�ߣ=�Q� -...- ���J��g6�0^��/ -...- '�%P�~�g��5p��'j -...- pqT��:��+Q��f b -...- v�wY��C%�m^�� -...- �+��ڛ?���l�q -...- ٯJ��aH@8���#C -...- �Zq�a�]����Sk� Y��.- P�
��
�6 -...- Rsd�cIJ#�o -...-
174.574219 MB Data written to o.mp4
Data written to File o.mp4 in 0.091976 secs,1990.251283 MB/sec
(fast) brian@localhost:/sdcard/Documents/Cxxdroid$
Test results on Cxxdroid
Testing Performance for 10 Millions times:
myChar cat : 0.147278 secs,67.898804 M/sec
Size :60.000000 M, 10 sample slices with witdth=32:
12345612345612345612345612345612 -...- 12345612345612345612345612345612 -...- 12345612345612345612345612345612 -...- 12345612345612345612345612345612 -...- 12345612345612345612345612345612 -...- 12345612345612345612345612345612 -...- 12345612345612345612345612345612 -...- 12345612345612345612345612345612 -...- 12345612345612345612345612345612 -...- 12345612345612345612345612345612 -...-
myChar get : 0.030455 secs,328.353308 M/sec
C++ string add ;0.129566 secs,77.180742 M/sec
C++ string get ;0.028515 secs,350.692618 M/sec
STD::vector push back ;0.498145 secs,20.074476 M/sec
STD::vector get ;0.028672 secs,348.772321 M/sec
STD::unordered_map set :5.756297 secs,1.737228 M/sec
STD::unordered_map get :1.829963 secs,5.464591 M/sec
STD::unordered_map string set :20.736899 secs,0.482232 M/sec
STD :: unordered_map string get : 11.860119 secs,0.843162 M/sec
C int array set :0.045866 secs,218.026425 M/sec
C int array get :0.031573 secs,316.726317 M/sec,
C char array set : 0.036120 secs,276.854928 M/sec
C char array get :0.031741 secs,315.049935 M/sec,
Testing Python 3.1.1 ,run loop for 1,000,000 Tines:
sh: python: not found
Testing download file from localhost and write to File with myChar:
Downloaed http://localhost:8000/static/in.mp4 in 0.657549 secs,278.390435 MB/sec
Size :183.055000 M, 20 sample slices with witdth=16:
Ʊm�̾���<�vb� -...- �ѡ���A���Ȋ� -...- �:���•ϳ��─ɢ ▲▼▼▼▲ D=�˘Q┬��␌�)�� ▲▼▼▼▲ ▼┼%"�8]%>�⎼�▼� ▲▼▼▼▲ ��K���[�(��� ▲▼▼▼▲ ���65ű8왗•��█├ ▲▼▼▼▲ �߃���│X┴�����T! ▲▼▼▼▲ ��<┐�^�@� ��I ▲▼▼▼▲ ┬ǭ──�⎻��5J#�� ▲▼▼▼▲ ;;Ռ��)�ߣ=�Q� ▲▼▼▼▲ ��J��±6�█^��/ ▲▼▼▼▲ '�%P�•�±��5⎻��'┘ ▲▼▼▼▲ ⎻─T��:��▶Q��° ␉ ▲▼▼▼▲ ┴�┬Y��C%�└^�� ▲▼▼▼▲ �▶��ڛ?���┌�─ ▲▼▼▼▲ ٯJ��▒H@8���#C ▲Y��▲ �Z─�▒�]����S┐� ▲▼▼▼▲ P
��
�6 ▲▼▼▼▲ R⎽␍�␌͔#�⎺ ▲▼▼▼▲
174▼574219 MB D▒├▒ ┬⎼␋├├␊┼ ├⎺ ⎺▼└⎻4
D▒├▒ ┬⎼␋├├␊┼ ├⎺ F␋┌␊ ⎺▼└⎻4 ␋┼ █▼3696█6 ⎽␊␌⎽◀495▼271592 MB/⎽␊␌
[P⎼⎺±⎼▒└ °␋┼␋⎽#␊␍]
沒有留言:
發佈留言