Ib me ntsis ntxiv txog kev xeem tsis zoo

Muaj ib hnub kuv tau hla dhau cov lej uas tus neeg siv tau sim saib xyuas RAM kev ua haujlwm hauv nws lub tshuab virtual. Kuv yuav tsis muab cov cai no (muaj "ko taw" nyob rau ntawd) thiab kuv yuav tawm tsuas yog qhov tseem ceeb tshaj plaws. Yog li, tus miv nyob hauv studio!

#include <sys/time.h>
#include <string.h>
#include <iostream>

#define CNT 1024
#define SIZE (1024*1024)

int main() {
	struct timeval start;
	struct timeval end;
	long millis;
	double gbs;
	char ** buffers;
	buffers = new char*[CNT];
	for (int i=0;i<CNT;i++) {
		buffers[i] = new char[SIZE];
	}
	gettimeofday(&start, NULL);
	for (int i=0;i<CNT;i++) {
		memset(buffers[i], 0, SIZE);
	}
	gettimeofday(&end, NULL);
	millis = (end.tv_sec - start.tv_sec) * 1000 +
		(end.tv_usec - start.tv_usec) / 1000;
	gbs = 1000.0 / millis;
	std::cout << gbs << " GB/sn";
	for (int i=0;i<CNT;i++) {
		delete buffers[i];
	}
	delete buffers;
	return 0;
}

Nws yooj yim - faib lub cim xeeb thiab sau ib gigabyte rau hauv nws. Thiab qhov kev sim no qhia li cas?

$ ./memtest
4.06504 GB / s

Kwv yees li 4 GB / s.

Dab tsi?!?!

Yuav ua li cas?!?!

Qhov no yog Core i7 (txawm tias tsis yog qhov tshiab tshaj), DDR4, processor yuav luag tsis thauj khoom - Vim li cas?!?!

Cov lus teb, raws li ib txwm, yog qhov txawv txav.

Tus neeg teb xov tooj tshiab (zoo li malloc muaj nuj nqi, los ntawm txoj kev) tsis tau faib lub cim xeeb. Nrog rau qhov kev hu no, tus faib saib cov npe ntawm qhov chaw pub dawb hauv lub cim xeeb pas dej, thiab yog tias tsis muaj, hu sbrk() kom nce cov ntaub ntawv ntu, thiab rov qab mus rau qhov kev pab cuam ib qho kev siv rau qhov chaw nyob los ntawm qhov chaw tshiab xwb. faib.

Qhov teeb meem yog tias thaj chaw faib yog virtual nkaus xwb. Tsis muaj nplooj ntawv nco tiag tiag raug faib.

Thiab thaum thawj zaug nkag mus rau txhua nplooj ntawv los ntawm ntu ntu no tshwm sim, MMU "tua" nplooj ntawv txhaum, tom qab uas nplooj ntawv virtual tau nkag mus tau raug muab rau ib qho tiag.

Yog li ntawd, qhov tseeb, peb tsis tau sim qhov kev ua tau zoo ntawm lub tsheb npav thiab RAM modules, tab sis kev ua haujlwm ntawm MMU thiab VMM ntawm kev ua haujlwm. Thiab txhawm rau txhawm rau ntsuas qhov ua tau zoo ntawm RAM, peb tsuas yog xav tau pib qhov chaw faib ib zaug. Piv txwv li no:

#include <sys/time.h>
#include <string.h>
#include <iostream>

#define CNT 1024
#define SIZE (1024*1024)

int main() {
	struct timeval start;
	struct timeval end;
	long millis;
	double gbs;
	char ** buffers;
	buffers = new char*[CNT];
	for (int i=0;i<CNT;i++) {
                // FIXED HERE!!!
		buffers[i] = new char[SIZE](); // Add brackets, &$# !!!
	}
	gettimeofday(&start, NULL);
	for (int i=0;i<CNT;i++) {
		memset(buffers[i], 0, SIZE);
	}
	gettimeofday(&end, NULL);
	millis = (end.tv_sec - start.tv_sec) * 1000 +
		(end.tv_usec - start.tv_usec) / 1000;
	gbs = 1000.0 / millis;
	std::cout << gbs << " GB/sn";
	for (int i=0;i<CNT;i++) {
		delete buffers[i];
	}
	delete buffers;
	return 0;
}

Ntawd yog, peb tsuas yog pib qhov faib buffers nrog tus nqi pib (char 0).

Peb tshawb xyuas:

$ ./memtest
28.5714 GB / s

Lwm yam.

Kev coj ncaj ncees ntawm zaj dab neeg - yog tias koj xav tau cov buffers loj los ua haujlwm sai, tsis txhob hnov ​​​​qab pib lawv.

Tau qhov twg los: www.hab.com

Ntxiv ib saib