Xin chào. Tôi đang cố viết mã một vấn đề trong đó tôi có hai vòng lặp. Một vòng lặp bên trong tính toán trung bình mẫu cho một tác nhân nhất định bằng cách sử dụng nhiều mô phỏng của một chức năng cụ thể [điều này không tầm thường, nhưng mỗi chức năng riêng lẻ cũng không quá chậm, mất khoảng một giây] và trong vòng lặp bên ngoài tôi lặp lại
Cách thực hành tốt nhất để song song cả vòng lặp bên trong và vòng lặp bên ngoài cùng một lúc là gì. Giả sử, đối với một tác nhân nhất định, tính toán song song một số mô phỏng và đồng thời tính toán song song giá trị cho các tác nhân khác nhau
using Random
const N = 1000
const S = 100000000
Data = rand[S,N]
function slow_inner_loop[data_n] # This function computes a sample average through Monte Carlo simulations
res_slow = zeros[S]
for s = 1 : S
res_slow[s] = data_n[s] # Here I have a function that computes a value for a given Monte Carlo Draw
end
return sum[res_slow]/S
end
function outer_loop[Data] # This function computes the sample average for N different "agents" and then computes a function with the computed averages
res_outer = zeros[N]
for n = 1 : N
res_outer[n] = slow_inner_loop[Data[:,n]]
end
return sum[res_outer]/N
end
outer_loop[Data]
Tôi có thể bắn vào nó, hãy để nó cho các bậc thầy [trong trường hợp này là FLoops]. Tôi đã chơi với nó bây giờ, và đây là những gì tôi nhận được ở đây
using Random
using FLoops
function mc[]
sleep[0.01]
return 0.5
end
function slow_inner_loop[data_n] # This function computes a sample average through Monte Carlo simulations
S = length[data_n]
res_slow = zeros[S]
for s = 1 : S
res_slow[s] = mc[]
end
return sum[res_slow]/S
end
function slow_inner_loop_floops[data_n] # This function computes a sample average through Monte Carlo simulations
S = length[data_n]
@floop for s = 1 : S
mc_s = mc[]
@reduce[ res_slow = zero[eltype[data_n]] + mc_s ]
end
return sum[res_slow]/S
end
function outer_loop[Data] # This function computes the sample average for N different "agents" and then computes a function with the computed averages
N = size[Data,2]
res_outer = zeros[N]
for n = 1 : N
res_outer[n] = slow_inner_loop[Data[:,n]]
end
return sum[res_outer]/N
end
function outer_loop_floops[Data] # This function computes the sample average for N different "agents" and then computes a function with the computed averages
N = size[Data,2]
@floop for n = 1 : N
r_inner = slow_inner_loop[Data[:,n]]
@reduce[res_outer = zero[eltype[Data]] + r_inner]
end
return sum[res_outer]/N
end
function test[;N=10,S=100]
data = rand[S,N]
println["Serial"]
outer_loop[rand[10,5]]
@time [r1 = outer_loop[data]]
println["FLoops:"]
outer_loop_floops[rand[10,5]]
@time [r2 = outer_loop_floops[data]]
r1 ≈ r2
end
Với điều đó, tôi nhận được
julia> test[]
Serial
11.316991 seconds [5.09 k allocations: 172.938 KiB]
FLoops:
1.172280 seconds [5.15 k allocations: 175.453 KiB]
true
Điều đó có nghĩa là
# outer for loop
for element in sequence
# inner for loop
for element in sequence:
body of inner for loop
body of outer for loop
2 đã mất tổng thời gian từ 11 giây đến 1. 17 giây [ở đây có 8 luồng]. Tôi đoán macro không chỉ cải thiện song song ở đây, mà kết quả nói chung là tuyệt vời và có lẽ sự song song hóa lồng nhau ở đây là tốt nhất có thểCó cách nào để sử dụng
# outer for loop
for element in sequence
# inner for loop
for element in sequence:
body of inner for loop
body of outer for loop
3 ở vòng lặp bên trong và # outer for loop
for element in sequence
# inner for loop
for element in sequence:
body of inner for loop
body of outer for loop
4 ở vòng lặp bên ngoài không?Hoặc để làm điều này với
# outer for loop
for element in sequence
# inner for loop
for element in sequence:
body of inner for loop
body of outer for loop
5 thay vì đa luồng?Cung cấp một chút ngữ cảnh, giải pháp hiện tại dựa trên FLoops dành nhiều thời gian cho Bộ sưu tập rác và sử dụng nhiều bộ nhớ. Trong bài đăng này, @odow cung cấp một phiên bản dựa trên
# outer for loop
for element in sequence
# inner for loop
for element in sequence:
body of inner for loop
body of outer for loop
5 có tốc độ tương đương nhưng sử dụng ít bộ nhớ hơn nhiều Trong Python, vòng lặp bên trong vòng lặp được gọi là vòng lặp lồng nhau. Trong hướng dẫn này, chúng ta sẽ tìm hiểu về các vòng lặp lồng nhau trong Python với sự trợ giúp của các ví dụ
Ngoài ra, Giải quyết
- Bài tập vòng lặp Python
- Trắc nghiệm vòng lặp Python
Mục lục
Vòng lặp lồng nhau trong Python là gì?
Một vòng lặp lồng nhau là một vòng lặp bên trong thân của vòng lặp bên ngoài. Vòng lặp bên trong hoặc bên ngoài có thể là bất kỳ loại nào, chẳng hạn như vòng lặp while hoặc vòng lặp for. Ví dụ: vòng lặp
1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90 1003 bên ngoài có thể chứa vòng lặp
1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90 1004 và ngược lại
Vòng ngoài có thể chứa nhiều hơn một vòng trong. Không có giới hạn về chuỗi các vòng lặp
Trong vòng lặp lồng nhau, số lần lặp sẽ bằng số lần lặp ở vòng ngoài nhân với số lần lặp ở vòng trong
Trong mỗi lần lặp của vòng lặp bên ngoài, vòng lặp bên trong thực hiện tất cả các lần lặp của nó. Đối với mỗi lần lặp của một vòng lặp bên ngoài, vòng lặp bên trong sẽ bắt đầu lại và hoàn thành việc thực hiện của nó trước khi vòng lặp bên ngoài có thể tiếp tục đến lần lặp tiếp theo của nó
Vòng lặp lồng nhau thường được sử dụng để làm việc với cấu trúc dữ liệu đa chiều, chẳng hạn như in mảng hai chiều, lặp lại danh sách chứa danh sách lồng nhau
Vòng lặp lồng nhau là một phần của câu lệnh luồng điều khiển giúp bạn hiểu kiến thức cơ bản về Python
Python lồng nhau cho vòng lặp
Trong Python, vòng lặp for được sử dụng để lặp qua một chuỗi chẳng hạn như danh sách, chuỗi, bộ dữ liệu, các đối tượng có thể lặp khác như phạm vi
Cú pháp sử dụng vòng lặp lồng nhau trong Python
# outer for loop
for element in sequence
# inner for loop
for element in sequence:
body of inner for loop
body of outer for loop
Trong ví dụ này, chúng tôi đang sử dụng vòng lặp for bên trong vòng lặp
1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90 1003. Trong ví dụ này, chúng tôi đang in một bảng cửu chương gồm mười số đầu tiên
- Vòng lặp
1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90 100
3 bên ngoài sử dụng hàm range[] để lặp qua mười số đầu tiên - Vòng lặp
1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90 100
3 bên trong sẽ thực hiện mười lần cho mỗi số bên ngoài - Trong phần thân của vòng lặp bên trong, chúng tôi sẽ in phép nhân của số bên ngoài và số hiện tại
- Vòng lặp bên trong chẳng là gì ngoài phần thân của vòng lặp bên ngoài
Ví dụ. Viết chương trình vòng lặp
1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90 1003 lồng nhau để in bảng cửu chương trong Python
using Random
using FLoops
function mc[]
sleep[0.01]
return 0.5
end
function slow_inner_loop[data_n] # This function computes a sample average through Monte Carlo simulations
S = length[data_n]
res_slow = zeros[S]
for s = 1 : S
res_slow[s] = mc[]
end
return sum[res_slow]/S
end
function slow_inner_loop_floops[data_n] # This function computes a sample average through Monte Carlo simulations
S = length[data_n]
@floop for s = 1 : S
mc_s = mc[]
@reduce[ res_slow = zero[eltype[data_n]] + mc_s ]
end
return sum[res_slow]/S
end
function outer_loop[Data] # This function computes the sample average for N different "agents" and then computes a function with the computed averages
N = size[Data,2]
res_outer = zeros[N]
for n = 1 : N
res_outer[n] = slow_inner_loop[Data[:,n]]
end
return sum[res_outer]/N
end
function outer_loop_floops[Data] # This function computes the sample average for N different "agents" and then computes a function with the computed averages
N = size[Data,2]
@floop for n = 1 : N
r_inner = slow_inner_loop[Data[:,n]]
@reduce[res_outer = zero[eltype[Data]] + r_inner]
end
return sum[res_outer]/N
end
function test[;N=10,S=100]
data = rand[S,N]
println["Serial"]
outer_loop[rand[10,5]]
@time [r1 = outer_loop[data]]
println["FLoops:"]
outer_loop_floops[rand[10,5]]
@time [r2 = outer_loop_floops[data]]
r1 ≈ r2
end
0đầu ra
1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90 100
- Trong chương trình này, vòng lặp
1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90 100
3 bên ngoài lặp lại các số từ 1 đến 10.
30 trả về 10 số. Vậy tổng số lần lặp của vòng ngoài là 10using Random using FLoops function mc[] sleep[0.01] return 0.5 end function slow_inner_loop[data_n] # This function computes a sample average through Monte Carlo simulations S = length[data_n] res_slow = zeros[S] for s = 1 : S res_slow[s] = mc[] end return sum[res_slow]/S end function slow_inner_loop_floops[data_n] # This function computes a sample average through Monte Carlo simulations S = length[data_n] @floop for s = 1 : S mc_s = mc[] @reduce[ res_slow = zero[eltype[data_n]] + mc_s ] end return sum[res_slow]/S end function outer_loop[Data] # This function computes the sample average for N different "agents" and then computes a function with the computed averages N = size[Data,2] res_outer = zeros[N] for n = 1 : N res_outer[n] = slow_inner_loop[Data[:,n]] end return sum[res_outer]/N end function outer_loop_floops[Data] # This function computes the sample average for N different "agents" and then computes a function with the computed averages N = size[Data,2] @floop for n = 1 : N r_inner = slow_inner_loop[Data[:,n]] @reduce[res_outer = zero[eltype[Data]] + r_inner] end return sum[res_outer]/N end function test[;N=10,S=100] data = rand[S,N] println["Serial"] outer_loop[rand[10,5]] @time [r1 = outer_loop[data]] println["FLoops:"] outer_loop_floops[rand[10,5]] @time [r2 = outer_loop_floops[data]] r1 ≈ r2 end
- Trong lần lặp đầu tiên của vòng lặp lồng nhau, số là 1. Trong phần tiếp theo, nó 2. và cứ thế cho đến 10
- Tiếp theo, Đối với mỗi lần lặp của vòng lặp bên ngoài, vòng lặp bên trong sẽ thực hiện mười lần. Vòng lặp bên trong cũng sẽ thực hiện mười lần vì chúng tôi đang in bảng nhân lên đến mười
- Trong mỗi lần lặp của một vòng lặp bên trong, chúng tôi đã tính phép nhân của hai số
Vòng lặp lồng nhau để in mẫu
Một cách sử dụng phổ biến nhất khác của vòng lặp lồng nhau là in các mẫu số và ngôi sao khác nhau
Hãy xem cách sử dụng vòng lặp lồng nhau để in mẫu sau trong Python
Mẫu
using Random
using FLoops
function mc[]
sleep[0.01]
return 0.5
end
function slow_inner_loop[data_n] # This function computes a sample average through Monte Carlo simulations
S = length[data_n]
res_slow = zeros[S]
for s = 1 : S
res_slow[s] = mc[]
end
return sum[res_slow]/S
end
function slow_inner_loop_floops[data_n] # This function computes a sample average through Monte Carlo simulations
S = length[data_n]
@floop for s = 1 : S
mc_s = mc[]
@reduce[ res_slow = zero[eltype[data_n]] + mc_s ]
end
return sum[res_slow]/S
end
function outer_loop[Data] # This function computes the sample average for N different "agents" and then computes a function with the computed averages
N = size[Data,2]
res_outer = zeros[N]
for n = 1 : N
res_outer[n] = slow_inner_loop[Data[:,n]]
end
return sum[res_outer]/N
end
function outer_loop_floops[Data] # This function computes the sample average for N different "agents" and then computes a function with the computed averages
N = size[Data,2]
@floop for n = 1 : N
r_inner = slow_inner_loop[Data[:,n]]
@reduce[res_outer = zero[eltype[Data]] + r_inner]
end
return sum[res_outer]/N
end
function test[;N=10,S=100]
data = rand[S,N]
println["Serial"]
outer_loop[rand[10,5]]
@time [r1 = outer_loop[data]]
println["FLoops:"]
outer_loop_floops[rand[10,5]]
@time [r2 = outer_loop_floops[data]]
r1 ≈ r2
end
3Chương trình
using Random
using FLoops
function mc[]
sleep[0.01]
return 0.5
end
function slow_inner_loop[data_n] # This function computes a sample average through Monte Carlo simulations
S = length[data_n]
res_slow = zeros[S]
for s = 1 : S
res_slow[s] = mc[]
end
return sum[res_slow]/S
end
function slow_inner_loop_floops[data_n] # This function computes a sample average through Monte Carlo simulations
S = length[data_n]
@floop for s = 1 : S
mc_s = mc[]
@reduce[ res_slow = zero[eltype[data_n]] + mc_s ]
end
return sum[res_slow]/S
end
function outer_loop[Data] # This function computes the sample average for N different "agents" and then computes a function with the computed averages
N = size[Data,2]
res_outer = zeros[N]
for n = 1 : N
res_outer[n] = slow_inner_loop[Data[:,n]]
end
return sum[res_outer]/N
end
function outer_loop_floops[Data] # This function computes the sample average for N different "agents" and then computes a function with the computed averages
N = size[Data,2]
@floop for n = 1 : N
r_inner = slow_inner_loop[Data[:,n]]
@reduce[res_outer = zero[eltype[Data]] + r_inner]
end
return sum[res_outer]/N
end
function test[;N=10,S=100]
data = rand[S,N]
println["Serial"]
outer_loop[rand[10,5]]
@time [r1 = outer_loop[data]]
println["FLoops:"]
outer_loop_floops[rand[10,5]]
@time [r2 = outer_loop_floops[data]]
r1 ≈ r2
end
5- Trong chương trình này, vòng lặp bên ngoài là số hàng in.
- Số lượng hàng là năm, vì vậy vòng lặp bên ngoài sẽ thực hiện năm lần
- Tiếp theo, vòng lặp bên trong là tổng số cột trong mỗi hàng
- Đối với mỗi lần lặp của vòng lặp bên ngoài, số cột được tăng thêm 1
- Trong lần lặp đầu tiên của vòng lặp bên ngoài, số lượng cột là 1, trong lần tiếp theo là 2. và như thế
- Vòng lặp bên trong bằng với số lượng cột
- Trong mỗi lần lặp của một vòng lặp bên trong, chúng tôi in dấu sao
Vòng lặp while bên trong vòng lặp for
Việc sử dụng một loại vòng lặp bên trong một loại vòng lặp khác là rất phổ biến và hữu ích. chúng ta có thể đặt một vòng lặp while bên trong vòng lặp
1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90 1003
Giả sử chúng tôi muốn lặp lại mỗi tên trong danh sách năm lần
- Ở đây chúng tôi sẽ lặp lại danh sách bằng cách sử dụng vòng lặp for bên ngoài
- Trong mỗi lần lặp của vòng lặp for bên ngoài, vòng lặp for bên trong thực hiện năm lần để in tên hiện tại năm lần
using Random
using FLoops
function mc[]
sleep[0.01]
return 0.5
end
function slow_inner_loop[data_n] # This function computes a sample average through Monte Carlo simulations
S = length[data_n]
res_slow = zeros[S]
for s = 1 : S
res_slow[s] = mc[]
end
return sum[res_slow]/S
end
function slow_inner_loop_floops[data_n] # This function computes a sample average through Monte Carlo simulations
S = length[data_n]
@floop for s = 1 : S
mc_s = mc[]
@reduce[ res_slow = zero[eltype[data_n]] + mc_s ]
end
return sum[res_slow]/S
end
function outer_loop[Data] # This function computes the sample average for N different "agents" and then computes a function with the computed averages
N = size[Data,2]
res_outer = zeros[N]
for n = 1 : N
res_outer[n] = slow_inner_loop[Data[:,n]]
end
return sum[res_outer]/N
end
function outer_loop_floops[Data] # This function computes the sample average for N different "agents" and then computes a function with the computed averages
N = size[Data,2]
@floop for n = 1 : N
r_inner = slow_inner_loop[Data[:,n]]
@reduce[res_outer = zero[eltype[Data]] + r_inner]
end
return sum[res_outer]/N
end
function test[;N=10,S=100]
data = rand[S,N]
println["Serial"]
outer_loop[rand[10,5]]
@time [r1 = outer_loop[data]]
println["FLoops:"]
outer_loop_floops[rand[10,5]]
@time [r2 = outer_loop_floops[data]]
r1 ≈ r2
end
7đầu ra
using Random
using FLoops
function mc[]
sleep[0.01]
return 0.5
end
function slow_inner_loop[data_n] # This function computes a sample average through Monte Carlo simulations
S = length[data_n]
res_slow = zeros[S]
for s = 1 : S
res_slow[s] = mc[]
end
return sum[res_slow]/S
end
function slow_inner_loop_floops[data_n] # This function computes a sample average through Monte Carlo simulations
S = length[data_n]
@floop for s = 1 : S
mc_s = mc[]
@reduce[ res_slow = zero[eltype[data_n]] + mc_s ]
end
return sum[res_slow]/S
end
function outer_loop[Data] # This function computes the sample average for N different "agents" and then computes a function with the computed averages
N = size[Data,2]
res_outer = zeros[N]
for n = 1 : N
res_outer[n] = slow_inner_loop[Data[:,n]]
end
return sum[res_outer]/N
end
function outer_loop_floops[Data] # This function computes the sample average for N different "agents" and then computes a function with the computed averages
N = size[Data,2]
@floop for n = 1 : N
r_inner = slow_inner_loop[Data[:,n]]
@reduce[res_outer = zero[eltype[Data]] + r_inner]
end
return sum[res_outer]/N
end
function test[;N=10,S=100]
data = rand[S,N]
println["Serial"]
outer_loop[rand[10,5]]
@time [r1 = outer_loop[data]]
println["FLoops:"]
outer_loop_floops[rand[10,5]]
@time [r2 = outer_loop_floops[data]]
r1 ≈ r2
end
8Luyện tập. In mẫu hình chữ nhật có 5 hàng 3 cột hình ngôi sao
Giải bài tập vòng lặp lồng nhau trong Python dưới đây
In các ngôi sao hình chữ nhật sau
using Random
using FLoops
function mc[]
sleep[0.01]
return 0.5
end
function slow_inner_loop[data_n] # This function computes a sample average through Monte Carlo simulations
S = length[data_n]
res_slow = zeros[S]
for s = 1 : S
res_slow[s] = mc[]
end
return sum[res_slow]/S
end
function slow_inner_loop_floops[data_n] # This function computes a sample average through Monte Carlo simulations
S = length[data_n]
@floop for s = 1 : S
mc_s = mc[]
@reduce[ res_slow = zero[eltype[data_n]] + mc_s ]
end
return sum[res_slow]/S
end
function outer_loop[Data] # This function computes the sample average for N different "agents" and then computes a function with the computed averages
N = size[Data,2]
res_outer = zeros[N]
for n = 1 : N
res_outer[n] = slow_inner_loop[Data[:,n]]
end
return sum[res_outer]/N
end
function outer_loop_floops[Data] # This function computes the sample average for N different "agents" and then computes a function with the computed averages
N = size[Data,2]
@floop for n = 1 : N
r_inner = slow_inner_loop[Data[:,n]]
@reduce[res_outer = zero[eltype[Data]] + r_inner]
end
return sum[res_outer]/N
end
function test[;N=10,S=100]
data = rand[S,N]
println["Serial"]
outer_loop[rand[10,5]]
@time [r1 = outer_loop[data]]
println["FLoops:"]
outer_loop_floops[rand[10,5]]
@time [r2 = outer_loop_floops[data]]
r1 ≈ r2
end
9Giải thêm bài tập vòng lặp
Hiển thị giải pháp
julia> test[]
Serial
11.316991 seconds [5.09 k allocations: 172.938 KiB]
FLoops:
1.172280 seconds [5.15 k allocations: 175.453 KiB]
true
0Phá vỡ vòng lặp lồng nhau
Câu lệnh break được sử dụng bên trong vòng lặp để thoát khỏi vòng lặp. Nếu the được sử dụng bên trong một vòng lặp lồng nhau [vòng lặp bên trong một vòng lặp khác], nó sẽ
Trong ví dụ sau, chúng ta có hai vòng lặp. Vòng lặp
1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90 1003 bên ngoài lặp lại bốn số đầu tiên bằng cách sử dụng hàm
using Random
using FLoops
function mc[]
sleep[0.01]
return 0.5
end
function slow_inner_loop[data_n] # This function computes a sample average through Monte Carlo simulations
S = length[data_n]
res_slow = zeros[S]
for s = 1 : S
res_slow[s] = mc[]
end
return sum[res_slow]/S
end
function slow_inner_loop_floops[data_n] # This function computes a sample average through Monte Carlo simulations
S = length[data_n]
@floop for s = 1 : S
mc_s = mc[]
@reduce[ res_slow = zero[eltype[data_n]] + mc_s ]
end
return sum[res_slow]/S
end
function outer_loop[Data] # This function computes the sample average for N different "agents" and then computes a function with the computed averages
N = size[Data,2]
res_outer = zeros[N]
for n = 1 : N
res_outer[n] = slow_inner_loop[Data[:,n]]
end
return sum[res_outer]/N
end
function outer_loop_floops[Data] # This function computes the sample average for N different "agents" and then computes a function with the computed averages
N = size[Data,2]
@floop for n = 1 : N
r_inner = slow_inner_loop[Data[:,n]]
@reduce[res_outer = zero[eltype[Data]] + r_inner]
end
return sum[res_outer]/N
end
function test[;N=10,S=100]
data = rand[S,N]
println["Serial"]
outer_loop[rand[10,5]]
@time [r1 = outer_loop[data]]
println["FLoops:"]
outer_loop_floops[rand[10,5]]
@time [r2 = outer_loop_floops[data]]
r1 ≈ r2
end
30 và vòng lặp 1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90 1003 bên trong cũng lặp lại bốn số đầu tiên. Nếu số bên ngoài và số hiện tại của vòng lặp bên trong giống nhau, thì ngắt vòng lặp bên trong [lồng nhau]
Ví dụ
julia> test[]
Serial
11.316991 seconds [5.09 k allocations: 172.938 KiB]
FLoops:
1.172280 seconds [5.15 k allocations: 175.453 KiB]
true
4đầu ra
using Random
using FLoops
function mc[]
sleep[0.01]
return 0.5
end
function slow_inner_loop[data_n] # This function computes a sample average through Monte Carlo simulations
S = length[data_n]
res_slow = zeros[S]
for s = 1 : S
res_slow[s] = mc[]
end
return sum[res_slow]/S
end
function slow_inner_loop_floops[data_n] # This function computes a sample average through Monte Carlo simulations
S = length[data_n]
@floop for s = 1 : S
mc_s = mc[]
@reduce[ res_slow = zero[eltype[data_n]] + mc_s ]
end
return sum[res_slow]/S
end
function outer_loop[Data] # This function computes the sample average for N different "agents" and then computes a function with the computed averages
N = size[Data,2]
res_outer = zeros[N]
for n = 1 : N
res_outer[n] = slow_inner_loop[Data[:,n]]
end
return sum[res_outer]/N
end
function outer_loop_floops[Data] # This function computes the sample average for N different "agents" and then computes a function with the computed averages
N = size[Data,2]
@floop for n = 1 : N
r_inner = slow_inner_loop[Data[:,n]]
@reduce[res_outer = zero[eltype[Data]] + r_inner]
end
return sum[res_outer]/N
end
function test[;N=10,S=100]
data = rand[S,N]
println["Serial"]
outer_loop[rand[10,5]]
@time [r1 = outer_loop[data]]
println["FLoops:"]
outer_loop_floops[rand[10,5]]
@time [r2 = outer_loop_floops[data]]
r1 ≈ r2
end
00Như bạn có thể thấy ở đầu ra, không có hàng nào chứa cùng một số
Tiếp tục Vòng lặp lồng nhau
Bỏ qua bước lặp hiện tại và chuyển sang bước lặp tiếp theo. Trong Python, khi gặp câu lệnh
using Random
using FLoops
function mc[]
sleep[0.01]
return 0.5
end
function slow_inner_loop[data_n] # This function computes a sample average through Monte Carlo simulations
S = length[data_n]
res_slow = zeros[S]
for s = 1 : S
res_slow[s] = mc[]
end
return sum[res_slow]/S
end
function slow_inner_loop_floops[data_n] # This function computes a sample average through Monte Carlo simulations
S = length[data_n]
@floop for s = 1 : S
mc_s = mc[]
@reduce[ res_slow = zero[eltype[data_n]] + mc_s ]
end
return sum[res_slow]/S
end
function outer_loop[Data] # This function computes the sample average for N different "agents" and then computes a function with the computed averages
N = size[Data,2]
res_outer = zeros[N]
for n = 1 : N
res_outer[n] = slow_inner_loop[Data[:,n]]
end
return sum[res_outer]/N
end
function outer_loop_floops[Data] # This function computes the sample average for N different "agents" and then computes a function with the computed averages
N = size[Data,2]
@floop for n = 1 : N
r_inner = slow_inner_loop[Data[:,n]]
@reduce[res_outer = zero[eltype[Data]] + r_inner]
end
return sum[res_outer]/N
end
function test[;N=10,S=100]
data = rand[S,N]
println["Serial"]
outer_loop[rand[10,5]]
@time [r1 = outer_loop[data]]
println["FLoops:"]
outer_loop_floops[rand[10,5]]
@time [r2 = outer_loop_floops[data]]
r1 ≈ r2
end
35 bên trong vòng lặp, nó sẽ bỏ qua tất cả các câu lệnh bên dưới nó và ngay lập tức chuyển sang bước lặp tiếp theoTrong ví dụ sau, chúng ta có hai vòng lặp. Vòng lặp for bên ngoài lặp lại danh sách đầu tiên và vòng lặp bên trong cũng lặp lại danh sách số thứ hai
Nếu số bên ngoài và số hiện tại của vòng lặp bên trong giống nhau, thì hãy chuyển sang lần lặp tiếp theo của vòng lặp bên trong
Ví dụ
using Random
using FLoops
function mc[]
sleep[0.01]
return 0.5
end
function slow_inner_loop[data_n] # This function computes a sample average through Monte Carlo simulations
S = length[data_n]
res_slow = zeros[S]
for s = 1 : S
res_slow[s] = mc[]
end
return sum[res_slow]/S
end
function slow_inner_loop_floops[data_n] # This function computes a sample average through Monte Carlo simulations
S = length[data_n]
@floop for s = 1 : S
mc_s = mc[]
@reduce[ res_slow = zero[eltype[data_n]] + mc_s ]
end
return sum[res_slow]/S
end
function outer_loop[Data] # This function computes the sample average for N different "agents" and then computes a function with the computed averages
N = size[Data,2]
res_outer = zeros[N]
for n = 1 : N
res_outer[n] = slow_inner_loop[Data[:,n]]
end
return sum[res_outer]/N
end
function outer_loop_floops[Data] # This function computes the sample average for N different "agents" and then computes a function with the computed averages
N = size[Data,2]
@floop for n = 1 : N
r_inner = slow_inner_loop[Data[:,n]]
@reduce[res_outer = zero[eltype[Data]] + r_inner]
end
return sum[res_outer]/N
end
function test[;N=10,S=100]
data = rand[S,N]
println["Serial"]
outer_loop[rand[10,5]]
@time [r1 = outer_loop[data]]
println["FLoops:"]
outer_loop_floops[rand[10,5]]
@time [r2 = outer_loop_floops[data]]
r1 ≈ r2
end
01đầu ra
using Random
using FLoops
function mc[]
sleep[0.01]
return 0.5
end
function slow_inner_loop[data_n] # This function computes a sample average through Monte Carlo simulations
S = length[data_n]
res_slow = zeros[S]
for s = 1 : S
res_slow[s] = mc[]
end
return sum[res_slow]/S
end
function slow_inner_loop_floops[data_n] # This function computes a sample average through Monte Carlo simulations
S = length[data_n]
@floop for s = 1 : S
mc_s = mc[]
@reduce[ res_slow = zero[eltype[data_n]] + mc_s ]
end
return sum[res_slow]/S
end
function outer_loop[Data] # This function computes the sample average for N different "agents" and then computes a function with the computed averages
N = size[Data,2]
res_outer = zeros[N]
for n = 1 : N
res_outer[n] = slow_inner_loop[Data[:,n]]
end
return sum[res_outer]/N
end
function outer_loop_floops[Data] # This function computes the sample average for N different "agents" and then computes a function with the computed averages
N = size[Data,2]
@floop for n = 1 : N
r_inner = slow_inner_loop[Data[:,n]]
@reduce[res_outer = zero[eltype[Data]] + r_inner]
end
return sum[res_outer]/N
end
function test[;N=10,S=100]
data = rand[S,N]
println["Serial"]
outer_loop[rand[10,5]]
@time [r1 = outer_loop[data]]
println["FLoops:"]
outer_loop_floops[rand[10,5]]
@time [r2 = outer_loop_floops[data]]
r1 ≈ r2
end
02Như bạn có thể thấy ở đầu ra, không có số nào giống nhau nhân với nhau
Các vòng lặp lồng nhau một dòng sử dụng hiểu danh sách
Ví dụ: nếu bạn có hai danh sách và muốn lấy tất cả các kết hợp của chúng, Để đạt được điều này, bạn cần sử dụng hai vòng lặp lồng nhau như được đề cập bên dưới
using Random
using FLoops
function mc[]
sleep[0.01]
return 0.5
end
function slow_inner_loop[data_n] # This function computes a sample average through Monte Carlo simulations
S = length[data_n]
res_slow = zeros[S]
for s = 1 : S
res_slow[s] = mc[]
end
return sum[res_slow]/S
end
function slow_inner_loop_floops[data_n] # This function computes a sample average through Monte Carlo simulations
S = length[data_n]
@floop for s = 1 : S
mc_s = mc[]
@reduce[ res_slow = zero[eltype[data_n]] + mc_s ]
end
return sum[res_slow]/S
end
function outer_loop[Data] # This function computes the sample average for N different "agents" and then computes a function with the computed averages
N = size[Data,2]
res_outer = zeros[N]
for n = 1 : N
res_outer[n] = slow_inner_loop[Data[:,n]]
end
return sum[res_outer]/N
end
function outer_loop_floops[Data] # This function computes the sample average for N different "agents" and then computes a function with the computed averages
N = size[Data,2]
@floop for n = 1 : N
r_inner = slow_inner_loop[Data[:,n]]
@reduce[res_outer = zero[eltype[Data]] + r_inner]
end
return sum[res_outer]/N
end
function test[;N=10,S=100]
data = rand[S,N]
println["Serial"]
outer_loop[rand[10,5]]
@time [r1 = outer_loop[data]]
println["FLoops:"]
outer_loop_floops[rand[10,5]]
@time [r2 = outer_loop_floops[data]]
r1 ≈ r2
end
03Bạn có thể viết mã nhanh và gọn hơn bằng cách sử dụng nén danh sách và vòng lặp lồng nhau như dưới đây
using Random
using FLoops
function mc[]
sleep[0.01]
return 0.5
end
function slow_inner_loop[data_n] # This function computes a sample average through Monte Carlo simulations
S = length[data_n]
res_slow = zeros[S]
for s = 1 : S
res_slow[s] = mc[]
end
return sum[res_slow]/S
end
function slow_inner_loop_floops[data_n] # This function computes a sample average through Monte Carlo simulations
S = length[data_n]
@floop for s = 1 : S
mc_s = mc[]
@reduce[ res_slow = zero[eltype[data_n]] + mc_s ]
end
return sum[res_slow]/S
end
function outer_loop[Data] # This function computes the sample average for N different "agents" and then computes a function with the computed averages
N = size[Data,2]
res_outer = zeros[N]
for n = 1 : N
res_outer[n] = slow_inner_loop[Data[:,n]]
end
return sum[res_outer]/N
end
function outer_loop_floops[Data] # This function computes the sample average for N different "agents" and then computes a function with the computed averages
N = size[Data,2]
@floop for n = 1 : N
r_inner = slow_inner_loop[Data[:,n]]
@reduce[res_outer = zero[eltype[Data]] + r_inner]
end
return sum[res_outer]/N
end
function test[;N=10,S=100]
data = rand[S,N]
println["Serial"]
outer_loop[rand[10,5]]
@time [r1 = outer_loop[data]]
println["FLoops:"]
outer_loop_floops[rand[10,5]]
@time [r2 = outer_loop_floops[data]]
r1 ≈ r2
end
04Làm thế nào để viết nó
- Đầu tiên, Viết một vòng lặp
1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90 100
3 bên ngoài sẽ lặp lại danh sách đầu tiên như
37using Random using FLoops function mc[] sleep[0.01] return 0.5 end function slow_inner_loop[data_n] # This function computes a sample average through Monte Carlo simulations S = length[data_n] res_slow = zeros[S] for s = 1 : S res_slow[s] = mc[] end return sum[res_slow]/S end function slow_inner_loop_floops[data_n] # This function computes a sample average through Monte Carlo simulations S = length[data_n] @floop for s = 1 : S mc_s = mc[] @reduce[ res_slow = zero[eltype[data_n]] + mc_s ] end return sum[res_slow]/S end function outer_loop[Data] # This function computes the sample average for N different "agents" and then computes a function with the computed averages N = size[Data,2] res_outer = zeros[N] for n = 1 : N res_outer[n] = slow_inner_loop[Data[:,n]] end return sum[res_outer]/N end function outer_loop_floops[Data] # This function computes the sample average for N different "agents" and then computes a function with the computed averages N = size[Data,2] @floop for n = 1 : N r_inner = slow_inner_loop[Data[:,n]] @reduce[res_outer = zero[eltype[Data]] + r_inner] end return sum[res_outer]/N end function test[;N=10,S=100] data = rand[S,N] println["Serial"] outer_loop[rand[10,5]] @time [r1 = outer_loop[data]] println["FLoops:"] outer_loop_floops[rand[10,5]] @time [r2 = outer_loop_floops[data]] r1 ≈ r2 end
- Tiếp theo, Viết một vòng lặp bên trong sẽ lặp lại danh sách thứ hai sau vòng lặp bên ngoài như
38using Random using FLoops function mc[] sleep[0.01] return 0.5 end function slow_inner_loop[data_n] # This function computes a sample average through Monte Carlo simulations S = length[data_n] res_slow = zeros[S] for s = 1 : S res_slow[s] = mc[] end return sum[res_slow]/S end function slow_inner_loop_floops[data_n] # This function computes a sample average through Monte Carlo simulations S = length[data_n] @floop for s = 1 : S mc_s = mc[] @reduce[ res_slow = zero[eltype[data_n]] + mc_s ] end return sum[res_slow]/S end function outer_loop[Data] # This function computes the sample average for N different "agents" and then computes a function with the computed averages N = size[Data,2] res_outer = zeros[N] for n = 1 : N res_outer[n] = slow_inner_loop[Data[:,n]] end return sum[res_outer]/N end function outer_loop_floops[Data] # This function computes the sample average for N different "agents" and then computes a function with the computed averages N = size[Data,2] @floop for n = 1 : N r_inner = slow_inner_loop[Data[:,n]] @reduce[res_outer = zero[eltype[Data]] + r_inner] end return sum[res_outer]/N end function test[;N=10,S=100] data = rand[S,N] println["Serial"] outer_loop[rand[10,5]] @time [r1 = outer_loop[data]] println["FLoops:"] outer_loop_floops[rand[10,5]] @time [r2 = outer_loop_floops[data]] r1 ≈ r2 end
- Cuối cùng, tính cộng số ngoài và số trong như
39using Random using FLoops function mc[] sleep[0.01] return 0.5 end function slow_inner_loop[data_n] # This function computes a sample average through Monte Carlo simulations S = length[data_n] res_slow = zeros[S] for s = 1 : S res_slow[s] = mc[] end return sum[res_slow]/S end function slow_inner_loop_floops[data_n] # This function computes a sample average through Monte Carlo simulations S = length[data_n] @floop for s = 1 : S mc_s = mc[] @reduce[ res_slow = zero[eltype[data_n]] + mc_s ] end return sum[res_slow]/S end function outer_loop[Data] # This function computes the sample average for N different "agents" and then computes a function with the computed averages N = size[Data,2] res_outer = zeros[N] for n = 1 : N res_outer[n] = slow_inner_loop[Data[:,n]] end return sum[res_outer]/N end function outer_loop_floops[Data] # This function computes the sample average for N different "agents" and then computes a function with the computed averages N = size[Data,2] @floop for n = 1 : N r_inner = slow_inner_loop[Data[:,n]] @reduce[res_outer = zero[eltype[Data]] + r_inner] end return sum[res_outer]/N end function test[;N=10,S=100] data = rand[S,N] println["Serial"] outer_loop[rand[10,5]] @time [r1 = outer_loop[data]] println["FLoops:"] outer_loop_floops[rand[10,5]] @time [r2 = outer_loop_floops[data]] r1 ≈ r2 end
- Cuối cùng, lưu trữ kết quả trong một danh sách mới như
50using Random using FLoops function mc[] sleep[0.01] return 0.5 end function slow_inner_loop[data_n] # This function computes a sample average through Monte Carlo simulations S = length[data_n] res_slow = zeros[S] for s = 1 : S res_slow[s] = mc[] end return sum[res_slow]/S end function slow_inner_loop_floops[data_n] # This function computes a sample average through Monte Carlo simulations S = length[data_n] @floop for s = 1 : S mc_s = mc[] @reduce[ res_slow = zero[eltype[data_n]] + mc_s ] end return sum[res_slow]/S end function outer_loop[Data] # This function computes the sample average for N different "agents" and then computes a function with the computed averages N = size[Data,2] res_outer = zeros[N] for n = 1 : N res_outer[n] = slow_inner_loop[Data[:,n]] end return sum[res_outer]/N end function outer_loop_floops[Data] # This function computes the sample average for N different "agents" and then computes a function with the computed averages N = size[Data,2] @floop for n = 1 : N r_inner = slow_inner_loop[Data[:,n]] @reduce[res_outer = zero[eltype[Data]] + r_inner] end return sum[res_outer]/N end function test[;N=10,S=100] data = rand[S,N] println["Serial"] outer_loop[rand[10,5]] @time [r1 = outer_loop[data]] println["FLoops:"] outer_loop_floops[rand[10,5]] @time [r2 = outer_loop_floops[data]] r1 ≈ r2 end
Hãy xem thêm các ví dụ như vậy
Trong ví dụ này, chúng tôi sẽ sử dụng hai vòng lặp
1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90 1003 trong phần Hiểu danh sách và kết quả cuối cùng sẽ là một danh sách các danh sách. chúng tôi sẽ không bao gồm các số giống nhau trong mỗi danh sách. chúng tôi sẽ lọc chúng bằng điều kiện if
using Random
using FLoops
function mc[]
sleep[0.01]
return 0.5
end
function slow_inner_loop[data_n] # This function computes a sample average through Monte Carlo simulations
S = length[data_n]
res_slow = zeros[S]
for s = 1 : S
res_slow[s] = mc[]
end
return sum[res_slow]/S
end
function slow_inner_loop_floops[data_n] # This function computes a sample average through Monte Carlo simulations
S = length[data_n]
@floop for s = 1 : S
mc_s = mc[]
@reduce[ res_slow = zero[eltype[data_n]] + mc_s ]
end
return sum[res_slow]/S
end
function outer_loop[Data] # This function computes the sample average for N different "agents" and then computes a function with the computed averages
N = size[Data,2]
res_outer = zeros[N]
for n = 1 : N
res_outer[n] = slow_inner_loop[Data[:,n]]
end
return sum[res_outer]/N
end
function outer_loop_floops[Data] # This function computes the sample average for N different "agents" and then computes a function with the computed averages
N = size[Data,2]
@floop for n = 1 : N
r_inner = slow_inner_loop[Data[:,n]]
@reduce[res_outer = zero[eltype[Data]] + r_inner]
end
return sum[res_outer]/N
end
function test[;N=10,S=100]
data = rand[S,N]
println["Serial"]
outer_loop[rand[10,5]]
@time [r1 = outer_loop[data]]
println["FLoops:"]
outer_loop_floops[rand[10,5]]
@time [r2 = outer_loop_floops[data]]
r1 ≈ r2
end
05đầu ra
using Random
using FLoops
function mc[]
sleep[0.01]
return 0.5
end
function slow_inner_loop[data_n] # This function computes a sample average through Monte Carlo simulations
S = length[data_n]
res_slow = zeros[S]
for s = 1 : S
res_slow[s] = mc[]
end
return sum[res_slow]/S
end
function slow_inner_loop_floops[data_n] # This function computes a sample average through Monte Carlo simulations
S = length[data_n]
@floop for s = 1 : S
mc_s = mc[]
@reduce[ res_slow = zero[eltype[data_n]] + mc_s ]
end
return sum[res_slow]/S
end
function outer_loop[Data] # This function computes the sample average for N different "agents" and then computes a function with the computed averages
N = size[Data,2]
res_outer = zeros[N]
for n = 1 : N
res_outer[n] = slow_inner_loop[Data[:,n]]
end
return sum[res_outer]/N
end
function outer_loop_floops[Data] # This function computes the sample average for N different "agents" and then computes a function with the computed averages
N = size[Data,2]
@floop for n = 1 : N
r_inner = slow_inner_loop[Data[:,n]]
@reduce[res_outer = zero[eltype[Data]] + r_inner]
end
return sum[res_outer]/N
end
function test[;N=10,S=100]
data = rand[S,N]
println["Serial"]
outer_loop[rand[10,5]]
@time [r1 = outer_loop[data]]
println["FLoops:"]
outer_loop_floops[rand[10,5]]
@time [r2 = outer_loop_floops[data]]
r1 ≈ r2
end
06Vòng lặp lồng nhau trong Python
Trong Python, Câu lệnh vòng lặp while lặp đi lặp lại việc thực thi một khối mã trong khi một điều kiện cụ thể là đúng. Chúng ta sử dụng vòng lặp w a while khi số lần lặp không cố định
Trong phần này, chúng ta sẽ xem cách sử dụng vòng lặp while bên trong vòng lặp while khác
Cú pháp để viết câu lệnh vòng lặp while lồng nhau trong Python như sau
using Random
using FLoops
function mc[]
sleep[0.01]
return 0.5
end
function slow_inner_loop[data_n] # This function computes a sample average through Monte Carlo simulations
S = length[data_n]
res_slow = zeros[S]
for s = 1 : S
res_slow[s] = mc[]
end
return sum[res_slow]/S
end
function slow_inner_loop_floops[data_n] # This function computes a sample average through Monte Carlo simulations
S = length[data_n]
@floop for s = 1 : S
mc_s = mc[]
@reduce[ res_slow = zero[eltype[data_n]] + mc_s ]
end
return sum[res_slow]/S
end
function outer_loop[Data] # This function computes the sample average for N different "agents" and then computes a function with the computed averages
N = size[Data,2]
res_outer = zeros[N]
for n = 1 : N
res_outer[n] = slow_inner_loop[Data[:,n]]
end
return sum[res_outer]/N
end
function outer_loop_floops[Data] # This function computes the sample average for N different "agents" and then computes a function with the computed averages
N = size[Data,2]
@floop for n = 1 : N
r_inner = slow_inner_loop[Data[:,n]]
@reduce[res_outer = zero[eltype[Data]] + r_inner]
end
return sum[res_outer]/N
end
function test[;N=10,S=100]
data = rand[S,N]
println["Serial"]
outer_loop[rand[10,5]]
@time [r1 = outer_loop[data]]
println["FLoops:"]
outer_loop_floops[rand[10,5]]
@time [r2 = outer_loop_floops[data]]
r1 ≈ r2
end
07Ví dụ
Trong ví dụ này, chúng tôi sẽ in 10 số đầu tiên trên mỗi dòng 5 lần
using Random
using FLoops
function mc[]
sleep[0.01]
return 0.5
end
function slow_inner_loop[data_n] # This function computes a sample average through Monte Carlo simulations
S = length[data_n]
res_slow = zeros[S]
for s = 1 : S
res_slow[s] = mc[]
end
return sum[res_slow]/S
end
function slow_inner_loop_floops[data_n] # This function computes a sample average through Monte Carlo simulations
S = length[data_n]
@floop for s = 1 : S
mc_s = mc[]
@reduce[ res_slow = zero[eltype[data_n]] + mc_s ]
end
return sum[res_slow]/S
end
function outer_loop[Data] # This function computes the sample average for N different "agents" and then computes a function with the computed averages
N = size[Data,2]
res_outer = zeros[N]
for n = 1 : N
res_outer[n] = slow_inner_loop[Data[:,n]]
end
return sum[res_outer]/N
end
function outer_loop_floops[Data] # This function computes the sample average for N different "agents" and then computes a function with the computed averages
N = size[Data,2]
@floop for n = 1 : N
r_inner = slow_inner_loop[Data[:,n]]
@reduce[res_outer = zero[eltype[Data]] + r_inner]
end
return sum[res_outer]/N
end
function test[;N=10,S=100]
data = rand[S,N]
println["Serial"]
outer_loop[rand[10,5]]
@time [r1 = outer_loop[data]]
println["FLoops:"]
outer_loop_floops[rand[10,5]]
@time [r2 = outer_loop_floops[data]]
r1 ≈ r2
end
08đầu ra
using Random
using FLoops
function mc[]
sleep[0.01]
return 0.5
end
function slow_inner_loop[data_n] # This function computes a sample average through Monte Carlo simulations
S = length[data_n]
res_slow = zeros[S]
for s = 1 : S
res_slow[s] = mc[]
end
return sum[res_slow]/S
end
function slow_inner_loop_floops[data_n] # This function computes a sample average through Monte Carlo simulations
S = length[data_n]
@floop for s = 1 : S
mc_s = mc[]
@reduce[ res_slow = zero[eltype[data_n]] + mc_s ]
end
return sum[res_slow]/S
end
function outer_loop[Data] # This function computes the sample average for N different "agents" and then computes a function with the computed averages
N = size[Data,2]
res_outer = zeros[N]
for n = 1 : N
res_outer[n] = slow_inner_loop[Data[:,n]]
end
return sum[res_outer]/N
end
function outer_loop_floops[Data] # This function computes the sample average for N different "agents" and then computes a function with the computed averages
N = size[Data,2]
@floop for n = 1 : N
r_inner = slow_inner_loop[Data[:,n]]
@reduce[res_outer = zero[eltype[Data]] + r_inner]
end
return sum[res_outer]/N
end
function test[;N=10,S=100]
data = rand[S,N]
println["Serial"]
outer_loop[rand[10,5]]
@time [r1 = outer_loop[data]]
println["FLoops:"]
outer_loop_floops[rand[10,5]]
@time [r2 = outer_loop_floops[data]]
r1 ≈ r2
end
09vòng lặp for bên trong vòng lặp While
Đôi khi sẽ hữu ích khi sử dụng một loại vòng lặp bên trong một loại vòng lặp khác. chúng ta có thể đặt một vòng lặp
1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90 1003 bên trong vòng lặp
1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90 1004
Giả sử chúng ta muốn in tất cả các số hoàn hảo từ 1 đến 100
- Ở đây chúng tôi sẽ lặp lại 100 số đầu tiên bằng cách sử dụng vòng lặp
1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90 100
4 - Trong mỗi lần lặp của vòng lặp
1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90 100
4 bên ngoài, vòng lặp1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90 100
3 bên trong thực hiện từ 1 đến số bên ngoài hiện tại để kiểm tra xem số hiện tại có phải là số hoàn hảo hay không
1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90 1000
Khi nào nên sử dụng Vòng lặp lồng nhau trong Python?
- Các vòng lặp lồng nhau rất hữu ích khi bạn có các mảng hoặc danh sách lồng nhau cần được lặp qua cùng một chức năng
- Khi bạn muốn in các mẫu sao và số khác nhau bằng cách sử dụng các hàng có thể các cột
Lưu ý đến sự phức tạp về thời gian. Hãy hiểu điều này với các ví dụ về cách hoạt động của vòng lặp lồng nhau trong Python
Chúng tôi sử dụng vòng lặp for để lặp lại trên các phần tử đã cho của một chuỗi hoặc có thể lặp lại. như
using Random
using FLoops
function mc[]
sleep[0.01]
return 0.5
end
function slow_inner_loop[data_n] # This function computes a sample average through Monte Carlo simulations
S = length[data_n]
res_slow = zeros[S]
for s = 1 : S
res_slow[s] = mc[]
end
return sum[res_slow]/S
end
function slow_inner_loop_floops[data_n] # This function computes a sample average through Monte Carlo simulations
S = length[data_n]
@floop for s = 1 : S
mc_s = mc[]
@reduce[ res_slow = zero[eltype[data_n]] + mc_s ]
end
return sum[res_slow]/S
end
function outer_loop[Data] # This function computes the sample average for N different "agents" and then computes a function with the computed averages
N = size[Data,2]
res_outer = zeros[N]
for n = 1 : N
res_outer[n] = slow_inner_loop[Data[:,n]]
end
return sum[res_outer]/N
end
function outer_loop_floops[Data] # This function computes the sample average for N different "agents" and then computes a function with the computed averages
N = size[Data,2]
@floop for n = 1 : N
r_inner = slow_inner_loop[Data[:,n]]
@reduce[res_outer = zero[eltype[Data]] + r_inner]
end
return sum[res_outer]/N
end
function test[;N=10,S=100]
data = rand[S,N]
println["Serial"]
outer_loop[rand[10,5]]
@time [r1 = outer_loop[data]]
println["FLoops:"]
outer_loop_floops[rand[10,5]]
@time [r2 = outer_loop_floops[data]]
r1 ≈ r2
end
57. Ở đây độ phức tạp của thời gian là O[n] vì chúng tôi đang lặp lại tất cả các mục từ danh sáchSố bước thực hiện [lặp đi lặp lại] xác định độ phức tạp về thời gian của một vòng lặp
Khi bạn sử dụng một vòng lặp lồng nhau và cả vòng lặp bên ngoài lẫn bên trong đều chạy mà không có bất kỳ điều kiện if nào trong đó, thì độ phức tạp về thời gian là
using Random
using FLoops
function mc[]
sleep[0.01]
return 0.5
end
function slow_inner_loop[data_n] # This function computes a sample average through Monte Carlo simulations
S = length[data_n]
res_slow = zeros[S]
for s = 1 : S
res_slow[s] = mc[]
end
return sum[res_slow]/S
end
function slow_inner_loop_floops[data_n] # This function computes a sample average through Monte Carlo simulations
S = length[data_n]
@floop for s = 1 : S
mc_s = mc[]
@reduce[ res_slow = zero[eltype[data_n]] + mc_s ]
end
return sum[res_slow]/S
end
function outer_loop[Data] # This function computes the sample average for N different "agents" and then computes a function with the computed averages
N = size[Data,2]
res_outer = zeros[N]
for n = 1 : N
res_outer[n] = slow_inner_loop[Data[:,n]]
end
return sum[res_outer]/N
end
function outer_loop_floops[Data] # This function computes the sample average for N different "agents" and then computes a function with the computed averages
N = size[Data,2]
@floop for n = 1 : N
r_inner = slow_inner_loop[Data[:,n]]
@reduce[res_outer = zero[eltype[Data]] + r_inner]
end
return sum[res_outer]/N
end
function test[;N=10,S=100]
data = rand[S,N]
println["Serial"]
outer_loop[rand[10,5]]
@time [r1 = outer_loop[data]]
println["FLoops:"]
outer_loop_floops[rand[10,5]]
@time [r2 = outer_loop_floops[data]]
r1 ≈ r2
end
58 bởi vì, đối với tất cả n phần tử, mã được thực thi n lầnVí dụ
1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90 1001
đầu ra
1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90 1002
Nếu bạn đưa ra một điều kiện trong vòng lặp bên trong sẽ dừng thực hiện sau một số phần tử và không thực hiện hết n lần lặp của vòng lặp bên trong hoặc vòng lặp bên ngoài, nó sẽ ít phức tạp hơn về thời gian
Sử dụng vòng lặp lồng nhau khi bạn không có lựa chọn thay thế nào tốt hơn, Hãy nhớ rằng viết mã hiệu quả và nhỏ gọn tốt hơn nhiều so với viết mã phức tạp