Created
July 2, 2015 07:36
-
-
Save koshigoe/fdb25fdce655e3686d96 to your computer and use it in GitHub Desktop.
ファイルの文字コード変換のメモリ使用量を調べる、的な
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'objspace' | |
| require 'csv' | |
| require 'nkf' | |
| require 'tempfile' | |
| def test_a | |
| src_encoding = NKF.guess(File.read(ARGV[0], 1_048_576)) | |
| case src_encoding | |
| when Encoding::US_ASCII, Encoding::UTF_8 | |
| 'UTF-8' | |
| when Encoding::Shift_JIS, Encoding::WINDOWS_31J | |
| 'CP932' | |
| when Encoding::EUC_JP | |
| 'EUC-JP' | |
| else | |
| raise "Invalid encoding of source_file." | |
| end | |
| open(ARGV[0], "r:#{src_encoding}:UTF-8", undef: :replace, replace: '') do |f| | |
| CSV.new(f, col_sep: '|').each do |row| | |
| puts `ps -o rss= -p #{Process.pid}`.to_i | |
| end | |
| end | |
| end | |
| def test_b | |
| Tempfile.open("encoded_file") do |temp| | |
| File.open(ARGV[0]) do |f| | |
| while !f.eof? | |
| chunk = f.read(1_048_576) | |
| while (one_byte = f.read(1)) != "\n" && !f.eof? | |
| chunk << one_byte | |
| end | |
| temp.puts chunk.encode('UTF-8', 'CP932', undef: :replace, replace: '') | |
| chunk.encode!('UTF-8', 'CP932', undef: :replace, replace: '') | |
| $stdout.puts `ps -o rss= -p #{Process.pid}`.to_i | |
| chunk.clear | |
| end | |
| end | |
| end | |
| end | |
| # puts ObjectSpace.memsize_of_all | |
| puts `ps -o rss= -p #{Process.pid}`.to_i | |
| test_b | |
| # puts ObjectSpace.memsize_of_all | |
| puts `ps -o rss= -p #{Process.pid}`.to_i |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment