Comparision Among The Access Speed Of Cache , Memory And Disk
2010-09-17 21:40
549 查看
The first homework of db design is to compare the speed of accessing cache , meory and disk. This experiment show the gap among them.
To test the accecc time of cache , I use cpu-z to find that the block size in my cpu cache is 64 byte. That means I can create a 64-byte-size array and access it repeatly to estimate the accecc time.
The block size in my main memory with win7 is 4k. So i make a array and access it every 4k byte sequentially.
To obtain the time of accessing disk, a large file will be readed every at intervals of 4k byte.
here i show the c++ code:
result:
The time to access cache is : 4.88889ns
The time to access memory is : 0.014us
The time to access disk is : 0.01ms
To test the accecc time of cache , I use cpu-z to find that the block size in my cpu cache is 64 byte. That means I can create a 64-byte-size array and access it repeatly to estimate the accecc time.
The block size in my main memory with win7 is 4k. So i make a array and access it every 4k byte sequentially.
To obtain the time of accessing disk, a large file will be readed every at intervals of 4k byte.
here i show the c++ code:
// Compare.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <ctime> #include <iostream> #include <fstream> using namespace std; #define CACHE_TIMES 9000 #define CACHE_BLOCK_SIZE 64 #define MEMORY_TIMES 1000 #define DISK_TIMES 100 #define MEMORY_BLOCK_SIZE 4096 clock_t start, stop; /* clock_t is a built-in type for processor time (ticks) */ double duration; /* records the run time (seconds) of a function */ void TestCache () { char test[CACHE_TIMES]; start = clock(); for (int i = 0 ; i < CACHE_TIMES*1000 ; i++) { test[i%CACHE_BLOCK_SIZE]++; } stop = clock(); duration = ((double)(stop - start)*1000000)/(CLK_TCK*CACHE_TIMES); cout << "The time to access cache is : " << duration <<"ns"<< endl; } void TestMemory() { char test[MEMORY_TIMES*CACHE_BLOCK_SIZE]; char dat; start = clock(); for (int i = 0 ; i < MEMORY_TIMES*1000 ; i++) { dat = test[(i*CACHE_BLOCK_SIZE)%(MEMORY_TIMES*CACHE_BLOCK_SIZE)]; } stop = clock(); duration = ((double)(stop - start)*1000)/(CLK_TCK*MEMORY_TIMES); cout << "The time to access memory is : " << duration <<"us"<< endl; } void TestDisk() { ofstream test("test.dat"); char dat; char buf[20]; for(int i = 0 ; i < DISK_TIMES*MEMORY_BLOCK_SIZE ; i ++) { dat = i % 256; test << dat; } test.close(); FILE *fp; start = clock(); fp=fopen("test.dat","r"); for (long i = 0 ; i < DISK_TIMES ; i++) { fseek(fp,i*MEMORY_BLOCK_SIZE,SEEK_SET); fread (buf , 1 , 1, fp); } fclose(fp); stop = clock(); duration = ((double)(stop - start)*1000)/(CLK_TCK*DISK_TIMES); cout << "The time to access disk is : " <<duration <<"ms"<< endl; } int _tmain(int argc, _TCHAR* argv[]) { TestCache(); TestMemory(); TestDisk(); getchar(); return 0; }
result:
The time to access cache is : 4.88889ns
The time to access memory is : 0.014us
The time to access disk is : 0.01ms
相关文章推荐
- the macro of watch variable value and its memory content
- Inprove the speed of the "Mass storage" as u disk
- The new stored format of Docker image on disk and Distribution
- DownLoadManager[20530:228829] DiskImageCache: Could not resolve the absolute path of the old directory.
- Allocationand Reuse of Memory in the Shared Pool
- [转]Installing, Configuring and Using Windows Server AppFabric and the "Velocity" Memory Cache in 10 minutes
- My priliminary understanding of the relationship between virtual address and physical memory address
- How to properly use 'dd' to benchmark the write speed of your disk?
- springboot tomcat8 duplicate springSecurityFilterChain and increasing the maximum size of the cache
- 每日一命令(13)free - (Display amount of free and used memory in the system)
- 【转】Description of the hard disk cache
- Memory access ordering part 2 - barriers and the Linux kernel
- gem5: the trace of cache set and cache line write hit counters
- How to properly use &#39;dd&#39; to benchmark the write speed of your disk?
- Question 12: In C++, which of the following is the best declaration for an overloaded operator[] to allow read-only access (and
- Linux change the speed and duplex settings of an Ethernet card
- Discover the difference among Types: is operator,typeof keyword and GetType method
- Under the Hoods of Cache Fusion, GES, GRD and GCS
- Northwind Starter Kit Review: Data Access and the essence of needless work, Part II
- Under the Hoods of Cache Fusion, GES, GRD and GCS