Skip to content

Commit 0a20567

Browse files
committed
统计某个文件夹下所有文件行数
1 parent 5d1bb37 commit 0a20567

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

count_lines.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# count the lines in a file from a dir
2+
def count_lines
3+
lines = 0
4+
fo = 0
5+
files = Dir["./*"]
6+
files.map do |file|
7+
f = File.open("#{file}", "r", encoding:"utf-8")
8+
f.each_line{lines += 1}
9+
File.foreach("#{file}") {fo += 1}
10+
o = IO.readlines("#{file}", encoding:"utf-8") # 每个文件变成一个数组,每一行变成数组的一个元素
11+
end
12+
p fo
13+
lines
14+
end
15+
16+
p "这个文件夹有#{count_lines}行代码."
17+
18+
# *** In ruby, file.readlines.each not faster than file.open.each_line, why?
19+
20+
# Both readlines and open.each_line read the file only once. And Ruby will do buffering on IO objects,
21+
# so it will read a block (e.g. 64KB) data from disk every time to minimize the cost on disk read.
22+
# There should be little time consuming difference in the disk read step.
23+
# When you call readlines, Ruby constructs an empty array [] and repeatedly reads a line of file contents and pushes it to the array.
24+
# And at last it will return the array containing all lines of the file.
25+
# When you call each_line, Ruby reads a line of file contents and yield it to your logic.
26+
# When you finished processing this line, ruby reads another line. It repeatedly reads lines until there is no more contents in the file.
27+
# The difference between the two method is that readlines have to append the lines to an array. When the file is large, Ruby might have to duplicate the underlying array (C level) to enlarge its size one or more times.
28+
# Digging into the source, readlines is implemented by io_s_readlines which calls rb_io_readlines. rb_io_readlines calls rb_io_getline_1 to fetch line and rb_ary_push to push result into the returning array.
29+
# each_line is implemented by rb_io_each_line which calls rb_io_getline_1 to fetch line just like readlines and yield the line to your logic with rb_yield.
30+
#So, there is no need to store line results in a growing array for each_line, no array resizing, copying issue.
31+
32+
# *** don't use read, because it read the whole file, it's better to use open or foreach

quick_sort.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
1+
# different quick sort
2+
13
def quick_sort1(a)
24
return a if a.size < 2
35
(x = a.pop) ? quick_sort1(a.select{|i| i <= x}) + [x] + quick_sort1(a.select{|i| i > x}) : []
46
end
57

8+
def quick_sort2(a)
9+
return a if a.size < 2
10+
left, right = a[1..-1].partition{ |y| y <= a.first } # 以第一个元素为第一次的比较元素x
11+
sort(left) + [ a.first ] + sort(right) # 左边部分继续递归排序, 右边部分继续递归排序。直到 a.size < 2.即直到a.size 为 1
12+
end
13+
14+
615
array = [72, 57, 88, 60, 42, 84, 73, 42, 85, 9, 102]
716

817
p quick_sort1(array)

0 commit comments

Comments
 (0)