2020q3 Quiz13
contributed by < StoneLin0708 >
LeetCode 1239. Maximum Length of a Concatenated String with Unique Characters
Given a C solution
Unfold
解釋上述程式碼運作原理
Because input characters contains only lower case English letters, therefore it is possible to encode character appearance of each input string into one 32bit integer as follows.
Given arrSize of inputs
Encode appearance to k
Where is
After convert string to character appearance, the unique character property can be simply verified by bitwise and equal zero, and string concatenation can be done by bitwise or.
The next step is to search the solution recursively by considering taking or to ignore on each string. will be taken and the solution will be updated, if the current solution still has a unique character set after concatenation, and there might be a potential better solution without current the , therefore it's required to store the previous solution and proceed to the next no matter if is taken or not.
改寫程式碼,避免使用遞迴並尋求效率更高的實作
Iterative version with linear allocator
Iterative version with staic array
- i7-8700 @ 4.6 ghz
- test data
- random generated 4096 dataset (which quality is very bad)
- a-p (i.e. ["a","b",…,"p"])
- perf measure time usage for 10000 times
- using valgrind for memory usage measurement
- gcc 9.3
|method|time 4096(ms)|memory 4096|time a-p(ms)|memory a-p|
|-|-|-|-|-|-|
|recursive|0.30073 ± 0.00028|37k|0.50186 ± 0.00023|37k|
|iterative dynamic|0.29658 ± 0.00025|64.5k|0.54361 ± 0.00049|37k|
|iterative static|0.28986 ± 0.00022|37k|0.41798 ± 0.00032|37k|
TODO:
- 預先建立測試資料,不依賴 LeetCode 線上評分系統 (常會遇到嚴重偏頗),而在可掌握的主機上,透過 perf 一類的工具,分析程式的執行效率和記憶體開銷
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
jserv
mmap
-
reference
cp source code
-
解釋上述程式碼運作原理
- open source and destination file, check file length by seeking to the end of file.
- fseek vs lseek , libc vs posix
- using mmap to map file to virtual memory address, also advice system to expect sequential access.
POSIX_MADV_SEQUENTIAL
The application expects to access the specified address range sequentially, running from lower addresses to higher addresses. Hence, pages in this region can be aggressively read ahead, and may be freed soon after they are accessed.
- read and write using memory addess
- close source memory map, synchronous memory to destination file and close destination memory map.
- close file
-
指出其缺失
- compare source and destination memory(mapped file) on each address, copy when necessay.
- has negative impact on most cast, only benefit in a case when source and destination are mostly same, which doesn't make sense.
- read and write using mmap rely on virtual memory, each time access to not loaded address trigger page fault, in my 16MB file copy test, it cause about 4800 page faults, which is a huge number compare to 140 page faults from plain cp, mmap method also take a lot longer.
page faults on |
16MB |
256MB |
mmap |
~4800 |
~76000 |
cp |
~145 |
~145 |
- outputfile - permission
- if output file doesn't exist, it always try create new file with permission 666
-
探討 sendfile 和 splice 等系統系統在上述程式的應用
- 參見 以 sendfile 和 splice 系統呼叫達到 Zero-Copy
Above article mentioned that mmap did reduce kernel space to user space copy, in a cost of VMM, and mmap approach could be slower in small file or sequential access, therefore it could be improved by sendfile.
Replace all mmap code with sendfile and test performance.
Sendfile copy take only 55 page faults to copy 256MB data, which is better than both cp and mmap