Skip to content

Commit 56ca071

Browse files
authored
Comparison between Mojo and Python
1 parent a75c36e commit 56ca071

File tree

7 files changed

+99
-57
lines changed

7 files changed

+99
-57
lines changed

problem-1/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Link: https://leetcode.com/problems/length-of-last-word/description/
44

5-
##Benchmarks
5+
# Benchmarks
66

77
Benchmark 1: python3 problem-1.py
88
Time (mean ± σ): 11.1 ms ± 1.6 ms [User: 9.4 ms, System: 1.8 ms]
@@ -14,7 +14,7 @@ Benchmark 2: mojo problem-1.mojo
1414
Time (mean ± σ): 222.3 ms ± 9.7 ms [User: 238.4 ms, System: 28.6 ms]
1515
Range (min … max): 205.5 ms … 238.0 ms 14 runs
1616

17-
##Which performed better?
17+
# Which performed better?
1818
'python3 problem-1.py' ran
1919
19.95 ± 3.02 times faster than 'mojo problem-1.mojo'
2020

problem-2/README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,17 @@
22

33
Link: https://leetcode.com/problems/student-attendance-record-i/description/
44

5-
## Benchmarks
5+
# Benchmarks
66

7-
| CPU | Time | Mem |
8-
|-------|-------|------|
7+
Benchmark 1: python3 problem-2.py
8+
Time (mean ± σ): 11.9 ms ± 2.2 ms [User: 9.8 ms, System: 2.2 ms]
9+
Range (min … max): 8.8 ms … 23.0 ms 316 runs
10+
11+
Benchmark 2: mojo problem-2.mojo
12+
Time (mean ± σ): 236.4 ms ± 5.3 ms [User: 259.6 ms, System: 21.9 ms]
13+
Range (min … max): 228.0 ms … 243.9 ms 12 runs
14+
15+
# Which performed better?
16+
'python3 problem-2.py' ran
17+
19.78 ± 3.59 times faster than 'mojo problem-2.mojo'
918

10-
Which performed better?

problem-2/problem-2.mojo

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
from python import Python
22

3-
def checkRecord():
3+
def checkRecord(attendance_record: String):
44
py = Python.import_module("builtins")
5-
attendance_record = py.input("Enter the attendance record: ")
6-
attendance_record_str = str(attendance_record)
5+
attendance_record_str = attendance_record
76

87
absent_count = 0
98
late_count = 0
@@ -18,13 +17,16 @@ def checkRecord():
1817
else:
1918
late_count = 0
2019

21-
if late_count >=3 or absent_count >= 2:
22-
py.print("The attendance record is not acceptable.")
20+
if late_count >= 3 or absent_count >= 2:
21+
print("The attendance record is not acceptable.")
2322
return False
2423

25-
py.print("The attendance record is acceptable.")
24+
print("The attendance record is acceptable.")
2625
return True
2726

28-
# Main function
27+
2928
def main():
30-
checkRecord()
29+
checkRecord("PAALP")
30+
31+
32+

problem-2/problem-2.py

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
1-
def isPalindrome(x):
2-
enternumber = x
1+
def checkRecord(attendance_record):
2+
3+
attendance_record_str = str(attendance_record)
34

4-
enternumber_str = str(enternumber)
5-
if not enternumber_str.isdigit():
6-
print("Invalid input. Please enter a valid integer.")
7-
return
5+
absent_count = 0
6+
late_count = 0
87

9-
store1 = [0] * len(enternumber_str)
10-
store2 = [0] * len(enternumber_str)
8+
for i in range(len(attendance_record_str)):
9+
10+
if attendance_record_str[i] == 'A':
11+
absent_count += 1
12+
late_count = 0
13+
elif attendance_record_str[i] == 'L':
14+
late_count += 1
15+
else:
16+
late_count = 0
1117

12-
for i in range(len(enternumber_str)):
13-
store1[i] = int(enternumber_str[i])
14-
print(store1[i])
15-
16-
for j in range(len(enternumber_str)-1, -1, -1):
17-
store2[len(enternumber_str)-1-j] = int(enternumber_str[j])
18-
19-
for i in range(len(enternumber_str)):
20-
print(store2[i])
21-
if store1[i] != store2[i]:
18+
if late_count >= 3 or absent_count >= 2:
19+
print("The attendance record is not acceptable.")
2220
return False
21+
22+
print("The attendance record is acceptable.")
2323
return True
2424

25+
# Main function
2526
def main():
26-
input_number = int(input("Enter an integer: "))
27-
result = isPalindrome(input_number)
28-
print("Is Palindrome:", result)
27+
checkRecord("PAALP")
28+
29+
main()

problem-4/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Problem 4
2+
3+
Link: https://leetcode.com/problems/detect-capital/description/
4+
5+
# Benchmarks
6+
7+
Benchmark 1: python3 problem-4.py
8+
Time (mean ± σ): 11.1 ms ± 1.3 ms [User: 9.6 ms, System: 1.7 ms]
9+
Range (min … max): 9.8 ms … 17.4 ms 260 runs
10+
11+
Warning: Statistical outliers were detected. Consider re-running this benchmark on a quiet system without any interferences from other programs. It might help to use the '--warmup' or '--prepare' options.
12+
13+
Benchmark 2: mojo problem-4.mojo
14+
Time (mean ± σ): 230.1 ms ± 10.1 ms [User: 249.7 ms, System: 26.7 ms]
15+
Range (min … max): 215.3 ms … 252.0 ms 13 runs
16+
17+
# Which performed better?
18+
'python3 problem-4.py' ran
19+
20.64 ± 2.52 times faster than 'mojo problem-4.mojo'

problem-4/problem-4.mojo

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
11
from python import Python
22

3-
def detectCapitalUse():
4-
let py = Python.import_module("builtins")
3+
def detectCapitalUse(word: String):
4+
py = Python.import_module("builtins")
55

6-
let user_input = py.input("Enter a word: ")
7-
let uinput=str(user_input)
6+
uinput = word
87

98
number_of_capital_letters = 0
10-
for letter in range(user_input):
9+
for i in range(len(uinput)):
10+
letter = uinput[i]
1111
if ord(letter) < 97:
1212
number_of_capital_letters += 1
1313

1414
number_of_small_letters = len(uinput) - number_of_capital_letters
1515

16-
if number_of_capital_letters == len(uinput) or number_of_small_letters == len(uinput) or (ord(user_input[0]) < 97 and number_of_capital_letters == 1):
17-
print(True)
16+
if number_of_capital_letters == len(uinput) or number_of_small_letters == len(uinput) or (ord(word[0]) < 97 and number_of_capital_letters == 1):
17+
return True
1818
else:
19-
print(False)
20-
21-
# print("Result:")
22-
#print(result)
19+
return False
2320

2421
def main():
25-
detectCapitalUse()
22+
result = detectCapitalUse("USA")
23+
print("Result:")
24+
print(result)
25+
26+
27+
28+
29+
30+
31+
32+
2633

2734

2835

problem-4/problem-4.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
1-
def detectCapitalUse():
2-
user_input = input("Enter a word: ")
1+
2+
def detectCapitalUse(word):
3+
34

5+
uinput = str(word)
6+
47
number_of_capital_letters = 0
5-
for letter in user_input:
8+
for i in range(len(uinput)):
9+
letter = uinput[i]
610
if ord(letter) < 97:
711
number_of_capital_letters += 1
812

9-
number_of_small_letters = len(user_input) - number_of_capital_letters
13+
number_of_small_letters = len(uinput) - number_of_capital_letters
1014

11-
if number_of_capital_letters == len(user_input) or number_of_small_letters == len(user_input) or (ord(user_input[0]) < 97 and number_of_capital_letters == 1):
12-
result = True
15+
if number_of_capital_letters == len(uinput) or number_of_small_letters == len(uinput) or (ord(word[0]) < 97 and number_of_capital_letters == 1):
16+
return True
1317
else:
14-
result = False
15-
16-
print("Result:", result)
18+
return False
1719

1820
def main():
19-
detectCapitalUse()
21+
result = detectCapitalUse("USA")
22+
print("Result:")
23+
print(result)
24+
main()

0 commit comments

Comments
 (0)