Skip to content

Commit ddae090

Browse files
committed
- More code.
1 parent bea2f7e commit ddae090

File tree

5 files changed

+99
-0
lines changed

5 files changed

+99
-0
lines changed

scripts/cpu-bound/cpu-bound.pl

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env perl
2+
3+
use v5.38;
4+
use threads;
5+
use Time::HiRes qw(time);
6+
7+
sub cpu_worker($id) {
8+
my $result = 0;
9+
for (my $i = 0; $i < 10_000_000; $i++) {
10+
$result += sqrt($i);
11+
}
12+
say "Thread $id completed.";
13+
}
14+
15+
my $start_time = time();
16+
my @threads = map { threads->create(\&cpu_worker, $_) } (1..9);
17+
$_->join for @threads;
18+
my $end_time = time();
19+
20+
printf("Total time taken: %.2f seconds\n", $end_time - $start_time);

scripts/cpu-bound/cpu-bound.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env python3
2+
3+
import time
4+
import math
5+
import threading
6+
7+
def cpu_worker(id):
8+
result = 0
9+
for i in range(10_000_000):
10+
result += math.sqrt(i)
11+
print(f"Thread {id} completed.")
12+
13+
start_time = time.time()
14+
threads = []
15+
for i in range(9):
16+
thread = threading.Thread(target=cpu_worker, args=(i + 1,))
17+
threads.append(thread)
18+
thread.start()
19+
for thread in threads:
20+
thread.join()
21+
end_time = time.time()
22+
print(f"Total time taken: {end_time - start_time:.2f} seconds")

scripts/io-bound/io-bound-new.pl

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env perl
2+
3+
use v5.38;
4+
use threads;
5+
use HTTP::Tiny;
6+
use Time::HiRes qw(time);
7+
8+
sub io_worker($id) {
9+
my $url = "https://www.google.com";
10+
my $content = HTTP::Tiny->new->get($url);
11+
say "Thread $id finished.";
12+
}
13+
14+
my $start_time = time();
15+
my @threads = map { threads->create(\&io_worker, $_) } (1..9);
16+
$_->join for @threads;
17+
my $end_time = time();
18+
printf("Total time taken: %.2f seconds\n", $end_time - $start_time);

scripts/io-bound/io-bound.pl

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env perl
2+
3+
use v5.38;
4+
use threads;
5+
use LWP::Simple;
6+
use Time::HiRes qw(time);
7+
8+
sub io_worker($id) {
9+
my $url = "https://www.google.com";
10+
my $content = get($url);
11+
say "Thread $id finished.";
12+
}
13+
14+
my $start_time = time();
15+
my @threads = map { threads->create(\&io_worker, $_) } (1..9);
16+
$_->join for @threads;
17+
my $end_time = time();
18+
printf("Total time taken: %.2f seconds\n", $end_time - $start_time);

scripts/io-bound/io-bound.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env python3
2+
3+
import time
4+
import requests
5+
import threading
6+
7+
def io_worker(id):
8+
url = "https://www.google.com"
9+
response = requests.get(url)
10+
print(f"Thread {id} finished.")
11+
12+
start_time = time.time()
13+
threads = []
14+
for i in range(9):
15+
thread = threading.Thread(target=io_worker, args=(i + 1,))
16+
threads.append(thread)
17+
thread.start()
18+
for thread in threads:
19+
thread.join()
20+
end_time = time.time()
21+
print(f"Total time taken: {end_time - start_time:.2f} seconds")

0 commit comments

Comments
 (0)