硬盘上生成一个 1G 大小内容为 a 的文件
http://all-things-zklhp.tk/?p=237* 先用python写了一下
time python -c "print 'a'*(1024*1024*1024)" > a.txt
用时 *2.2s*
* 编译型语言表现如何
** C++生成这种字符串是相对容易的
程序代码:
#include <iostream> #include <string> #include <fstream> using std::cout; using std::cin; using std::endl; using std::string; using std::ofstream; int main(void) { const size_t size = 1024*1024*1024; string tmp(size, L'a'); ofstream ofs("a.txt"); ofs << tmp; ofs.close(); return 0; }
用时 *2.4s*
怎么回事 竟然比python+重定向慢
** 稍微改改
程序代码:
#include <iostream> #include <string> using std::cout; using std::cin; using std::endl; using std::string; int main(void) { const size_t size = 1024*1024*1024; string tmp(size, L'a'); cout << tmp; return 0; }
用时 *2.2s*
当然 这样就和python一样了
** C语言
C语言实现比较底层
程序代码:
#include <stdio.h> #include <memory.h> #define BUF (4*1024) #define SIZE (1024*1024*1024) #define TIMES (SIZE/BUF) int main(void) { FILE *f = NULL; char tmp[BUF] = {0}; size_t i = 0; if ((f = fopen("a.txt", "w")) == NULL) return 0; memset(tmp, 'a', sizeof tmp); for (i = 0; i < TIMES; i++) fwrite(tmp, sizeof tmp, 1, f); fclose(f); return 0; }
调整每次写入的大小 时间不同 大约 *2s*
* 结论
虽然C语言最快 但我肯定用简单方法来实现
这里的瓶颈其实在IO上面 但也能看出来 不同语言实现上还是有细微差别的
不知道有没有其他实现方法呢?期待大家的代码和测试数据
* 2014-8-24 结论下得太早了
在Windows下 这种情况下应该用内存映射文件
#+BEGIN_SRC c
#include <stdio.h>
#include <memory.h>
#include <windows.h>
#define SIZE (1024*1024*1024)
int main(void)
{
HANDLE hFile = CreateFile(\
"a.txt", GENERIC_READ | GENERIC_WRITE, 0, \
NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
printf("CreateFile() fails.\n");
return -1;
}
HANDLE hFileMap = CreateFileMapping(\
hFile, NULL, PAGE_READWRITE, \
0, SIZE, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
printf("CreateFileMapping() fails.\n");
CloseHandle(hFile);
return -1;
}
PBYTE pbFile = (PBYTE) MapViewOfFile(\
hFileMap, FILE_MAP_WRITE, 0, 0, 0);
if (pbFile == NULL)
{
printf("MapViewOfFile() fails.\n");
CloseHandle(hFileMap);
CloseHandle(hFile);
}
memset(pbFile, 0x61616161, SIZE / 4);
UnmapViewOfFile(pbFile);
CloseHandle(hFileMap);
CloseHandle(hFile);
return 0;
}
#+END_SRC
如果文件需要新建 最快 *0.67s* 最慢 *1.5s*
如果文件已经存在 哪怕是个空文件 *0.15s*
** 看一下微软运行时的memset()
好像运行时的代码是公开的 但还是看一下
#+BEGIN_SRC asm
Dump of assembler code for function msvcrt!memset:
0x000007fb369b1060 <+0>: mov rax,rcx
0x000007fb369b1063 <+3>: cmp r8,0x8
0x000007fb369b1067 <+7>: jb 0x7fb369b10f4 <msvcrt!memset+148>
0x000007fb369b106d <+13>: movzx edx,dl
0x000007fb369b1070 <+16>: movabs r9,0x101010101010101
0x000007fb369b107a <+26>: imul rdx,r9
0x000007fb369b107e <+30>: cmp r8,0x40
0x000007fb369b1082 <+34>: jb 0x7fb369b10d8 <msvcrt!memset+120>
0x000007fb369b1084 <+36>: neg rcx
0x000007fb369b1087 <+39>: and ecx,0x7
0x000007fb369b108a <+42>: jne 0x7fb369b12b2 <msvcrt!free+274>
0x000007fb369b1090 <+48>: add rcx,rax
0x000007fb369b1093 <+51>: mov r9,r8
0x000007fb369b1096 <+54>: and r8,0x3f
0x000007fb369b109a <+58>: shr r9,0x6
0x000007fb369b109e <+62>: je 0x7fb369b10d8 <msvcrt!memset+120>
0x000007fb369b10a0 <+64>: cmp r9,0x1c00
0x000007fb369b10a7 <+71>: jae 0x7fb369b5df4 <toupper+20>
0x000007fb369b10ad <+77>: mov QWORD PTR [rcx],rdx
0x000007fb369b10b0 <+80>: mov QWORD PTR [rcx+0x8],rdx
0x000007fb369b10b4 <+84>: mov QWORD PTR [rcx+0x10],rdx
0x000007fb369b10b8 <+88>: add rcx,0x40
0x000007fb369b10bc <+92>: mov QWORD PTR [rcx-0x28],rdx
0x000007fb369b10c0 <+96>: mov QWORD PTR [rcx-0x20],rdx
0x000007fb369b10c4 <+100>: dec r9
0x000007fb369b10c7 <+103>: mov QWORD PTR [rcx-0x18],rdx
0x000007fb369b10cb <+107>: mov QWORD PTR [rcx-0x10],rdx
0x000007fb369b10cf <+111>: mov QWORD PTR [rcx-0x8],rdx
0x000007fb369b10d3 <+115>: jne 0x7fb369b10ad <msvcrt!memset+77>
0x000007fb369b10d5 <+117>: jmp 0x7fb369b10d8 <msvcrt!memset+120>
0x000007fb369b10d7 <+119>: int3
0x000007fb369b10d8 <+120>: mov r9,r8
0x000007fb369b10db <+123>: and r8,0x7
0x000007fb369b10df <+127>: shr r9,0x3
0x000007fb369b10e3 <+131>: je 0x7fb369b10f4 <msvcrt!memset+148>
0x000007fb369b10e5 <+133>: xchg ax,ax
0x000007fb369b10e7 <+135>: nop
0x000007fb369b10e8 <+136>: mov QWORD PTR [rcx],rdx
0x000007fb369b10eb <+139>: add rcx,0x8
0x000007fb369b10ef <+143>: dec r9
0x000007fb369b10f2 <+146>: jne 0x7fb369b10e8 <msvcrt!memset+136>
0x000007fb369b10f4 <+148>: test r8,r8
0x000007fb369b10f7 <+151>: jne 0x7fb369b1185 <msvcrt!memmove+133>
0x000007fb369b10fd <+157>: ret 0x0
Dump of assembler code for function toupper:
0x000007fb369b5de0 <+0>: cmp DWORD PTR [rip+0x89189],0x0 # 0x7fb36a3ef70 <msvcrt!_dstbias+32>
0x000007fb369b5de7 <+7>: je 0x7fb369d7674 <msvcrt!_ltow+40>
0x000007fb369b5ded <+13>: xor edx,edx
0x000007fb369b5def <+15>: jmp 0x7fb369bdfac <msvcrt!_toupper_l>
0x000007fb369b5df4 <+20>: movnti QWORD PTR [rcx],rdx
0x000007fb369b5df8 <+24>: movnti QWORD PTR [rcx+0x8],rdx
0x000007fb369b5dfd <+29>: movnti QWORD PTR [rcx+0x10],rdx
0x000007fb369b5e02 <+34>: add rcx,0x40
0x000007fb369b5e06 <+38>: movnti QWORD PTR [rcx-0x28],rdx
0x000007fb369b5e0b <+43>: movnti QWORD PTR [rcx-0x20],rdx
0x000007fb369b5e10 <+48>: dec r9
0x000007fb369b5e13 <+51>: movnti QWORD PTR [rcx-0x18],rdx
0x000007fb369b5e18 <+56>: movnti QWORD PTR [rcx-0x10],rdx
0x000007fb369b5e1d <+61>: movnti QWORD PTR [rcx-0x8],rdx
0x000007fb369b5e22 <+66>: jne 0x7fb369b5df4 <toupper+20>
0x000007fb369b5e24 <+68>: lock or BYTE PTR [rsp],0x0
0x000007fb369b5e29 <+73>: jmp 0x7fb369b10d8 <msvcrt!memset+120>
0x000007fb369b5e2e <+78>: int3
0x000007fb369b5e2f <+79>: nop
0x000007fb369b5e30 <+80>: nop
0x000007fb369b5e31 <+81>: nop
0x000007fb369b5e32 <+82>: nop
0x000007fb369b5e33 <+83>: nop
0x000007fb369b5e34 <+84>: nop
0x000007fb369b5e35 <+85>: nop
0x000007fb369b5e36 <+86>: nop
0x000007fb369b5e37 <+87>: nop
0x000007fb369b5e38 <+88>: mov QWORD PTR [rsp+0x8],rbx
0x000007fb369b5e3d <+93>: mov QWORD PTR [rsp+0x10],rsi
0x000007fb369b5e42 <+98>: mov DWORD PTR [rsp+0x18],r8d
0x000007fb369b5e47 <+103>: push rdi
0x000007fb369b5e48 <+104>: push r12
0x000007fb369b5e4a <+106>: push r13
0x000007fb369b5e4c <+108>: push r14
0x000007fb369b5e4e <+110>: push r15
0x000007fb369b5e50 <+112>: sub rsp,0x40
0x000007fb369b5e54 <+116>: mov r14d,r8d
0x000007fb369b5e57 <+119>: mov ebx,edx
0x000007fb369b5e59 <+121>: mov r12d,ecx
0x000007fb369b5e5c <+124>: test r8d,r8d
0x000007fb369b5e5f <+127>: jne 0x7fb369b5e6e <toupper+142>
0x000007fb369b5e61 <+129>: call 0x7fb369b616c <msvcrt!exit+16>
0x000007fb369b5e66 <+134>: test eax,eax
0x000007fb369b5e68 <+136>: jne 0x7fb369f0e7c <strncpy+42460>
0x000007fb369b5e6e <+142>: mov ecx,0x8
0x000007fb369b5e73 <+147>: call 0x7fb369b1828 <msvcrt!_lock>
0x000007fb369b5e78 <+152>: nop
0x000007fb369b5e79 <+153>: cmp DWORD PTR [rip+0x89100],0x1 # 0x7fb36a3ef80 <msvcrt!__lc_collate_cp+4>
0x000007fb369b5e80 <+160>: je 0x7fb369b5f42 <toupper+354>
0x000007fb369b5e86 <+166>: mov DWORD PTR [rip+0x89058],0x1 # 0x7fb36a3eee8 <msvcrt!_winmajor+8>
0x000007fb369b5e90 <+176>: mov BYTE PTR [rip+0x89061],r14b # 0x7fb36a3eef8 <msvcrt!_winmajor+24>
0x000007fb369b5e97 <+183>: test ebx,ebx
0x000007fb369b5e99 <+185>: jne 0x7fb369b5f42 <toupper+354>
0x000007fb369b5e9f <+191>: mov rcx,QWORD PTR [rip+0x8876a] # 0x7fb36a3e610 <msvcrt!_wcmdln+40>
0x000007fb369b5ea6 <+198>: call 0x7fb369b3d14 <msvcrt!_initterm+68>
0x000007fb369b5eab <+203>: mov rsi,rax
0x000007fb369b5eae <+206>: mov QWORD PTR [rsp+0x30],rax
0x000007fb369b5eb3 <+211>: test rax,rax
0x000007fb369b5eb6 <+214>: je 0x7fb369b5f42 <toupper+354>
0x000007fb369b5ebc <+220>: mov rcx,QWORD PTR [rip+0x88735] # 0x7fb36a3e5f8 <msvcrt!_wcmdln+16>
0x000007fb369b5ec3 <+227>: call 0x7fb369b3d14 <msvcrt!_initterm+68>
0x000007fb369b5ec8 <+232>: mov rdi,rax
0x000007fb369b5ecb <+235>: mov QWORD PTR [rsp+0x20],rax
0x000007fb369b5ed0 <+240>: mov r13,rsi
0x000007fb369b5ed3 <+243>: mov QWORD PTR [rsp+0x28],rsi
0x000007fb369b5ed8 <+248>: mov r15,rax
0x000007fb369b5edb <+251>: mov QWORD PTR [rsp+0x38],rax
0x000007fb369b5ee0 <+256>: sub rdi,0x8
0x000007fb369b5ee4 <+260>: mov QWORD PTR [rsp+0x20],rdi
0x000007fb369b5ee9 <+265>: cmp rdi,rsi
0x000007fb369b5eec <+268>: jb 0x7fb369b5f42 <toupper+354>
0x000007fb369b5eee <+270>: call 0x7fb369b3d04 <msvcrt!_initterm+52>
0x000007fb369b5ef3 <+275>: cmp QWORD PTR [rdi],rax
0x000007fb369b5ef6 <+278>: je 0x7fb369b5f3f <toupper+351>
0x000007fb369b5ef8 <+280>: cmp rdi,rsi
0x000007fb369b5efb <+283>: jb 0x7fb369b5f42 <toupper+354>
0x000007fb369b5efd <+285>: mov rcx,QWORD PTR [rdi]
0x000007fb369b5f00 <+288>: call 0x7fb369b3d14 <msvcrt!_initterm+68>
0x000007fb369b5f05 <+293>: mov rbx,rax
0x000007fb369b5f08 <+296>: call 0x7fb369b3d04 <msvcrt!_initterm+52>
0x000007fb369b5f0d <+301>: mov QWORD PTR [rdi],rax
0x000007fb369b5f10 <+304>: call rbx
0x000007fb369b5f12 <+306>: mov rcx,QWORD PTR [rip+0x886f7] # 0x7fb36a3e610 <msvcrt!_wcmdln+40>
0x000007fb369b5f19 <+313>: call 0x7fb369b3d14 <msvcrt!_initterm+68>
0x000007fb369b5f1e <+318>: mov rbx,rax
0x000007fb369b5f21 <+321>: mov rcx,QWORD PTR [rip+0x886d0] # 0x7fb36a3e5f8 <msvcrt!_wcmdln+16>
0x000007fb369b5f28 <+328>: call 0x7fb369b3d14 <msvcrt!_initterm+68>
0x000007fb369b5f2d <+333>: cmp r13,rbx
0x000007fb369b5f30 <+336>: jne 0x7fb369f0e8b <strncpy+42475>
0x000007fb369b5f36 <+342>: cmp r15,rax
0x000007fb369b5f39 <+345>: jne 0x7fb369f0e8b <strncpy+42475>
0x000007fb369b5f3f <+351>: jmp 0x7fb369b5ee0 <toupper+256>
0x000007fb369b5f41 <+353>: int3
0x000007fb369b5f42 <+354>: test r14d,r14d
0x000007fb369b5f45 <+357>: jne 0x7fb369bf6a9 <msvcrt!longjmp+121>
0x000007fb369b5f4b <+363>: mov DWORD PTR [rip+0x8902b],0x1 # 0x7fb36a3ef80 <msvcrt!__lc_collate_cp+4>
0x000007fb369b5f55 <+373>: mov ecx,0x8
0x000007fb369b5f5a <+378>: call 0x7fb369b1870 <msvcrt!_unlock>
0x000007fb369b5f5f <+383>: mov ecx,r12d
0x000007fb369b5f62 <+386>: call 0x7fb369b6118 <msvcrt!_rmtmp+344>
0x000007fb369b5f67 <+391>: mov ecx,r12d
0x000007fb369b5f6a <+394>: call QWORD PTR [rip+0x954a8] # 0x7fb36a4b418
0x000007fb369b5f70 <+400>: int3
0x000007fb369b5f71 <+401>: mov rbx,QWORD PTR [rsp+0x70]
0x000007fb369b5f76 <+406>: mov rsi,QWORD PTR [rsp+0x78]
0x000007fb369b5f7b <+411>: add rsp,0x40
0x000007fb369b5f7f <+415>: pop r15
0x000007fb369b5f81 <+417>: pop r14
0x000007fb369b5f83 <+419>: pop r13
0x000007fb369b5f85 <+421>: pop r12
0x000007fb369b5f87 <+423>: pop rdi
0x000007fb369b5f88 <+424>: ret
0x000007fb369b5f89 <+425>: nop
0x000007fb369b5f8a <+426>: nop
0x000007fb369b5f8b <+427>: nop
0x000007fb369b5f8c <+428>: nop
0x000007fb369b5f8d <+429>: nop
0x000007fb369b5f8e <+430>: nop
0x000007fb369b5f8f <+431>: nop
0x000007fb369b5f90 <+432>: sub rsp,0x28
0x000007fb369b5f94 <+436>: call 0x7fb369b5c40 <msvcrt!_flushall>
0x000007fb369b5f99 <+441>: cmp BYTE PTR [rip+0x88f58],0x0 # 0x7fb36a3eef8 <msvcrt!_winmajor+24>
0x000007fb369b5fa0 <+448>: jne 0x7fb369bf6de <msvcrt!longjmp+174>
0x000007fb369b5fa6 <+454>: mov rcx,QWORD PTR [rip+0x8889b] # 0x7fb36a3e848 <msvcrt!_environ+40>
0x000007fb369b5fad <+461>: add rsp,0x28
0x000007fb369b5fb1 <+465>: jmp 0x7fb369b11a0 <msvcrt!free>
0x000007fb369b5fb6 <+470>: int3
0x000007fb369b5fb7 <+471>: nop
0x000007fb369b5fb8 <+472>: nop
0x000007fb369b5fb9 <+473>: nop
0x000007fb369b5fba <+474>: nop
0x000007fb369b5fbb <+475>: nop
0x000007fb369b5fbc <+476>: nop
0x000007fb369b5fbd <+477>: nop
0x000007fb369b5fbe <+478>: nop
0x000007fb369b5fbf <+479>: nop
#+END_SRC
用到了SSE指令 不错
[ 本帖最后由 zklhp 于 2014-8-24 13:08 编辑 ]