Skip to content

Fixed Mojo + benchmark script #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.bin
22 changes: 22 additions & 0 deletions bench.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash
BENCH="hyperfine -i -N -w 10 "

for dir in *; do
if [ -d "$dir" ]; then
# Compile command with the name
COMPILE="mojo build ${dir}.mojo -o ${dir}.bin"
# Generate commands to run
COMMANDS=("python ${dir}.py")
COMMANDS+=("mojo ${dir}.mojo")
COMMANDS+=(${dir}.bin)
cd "$dir"
echo
echo "Benchmarking $dir"
echo "======================"
# Compile mojo first
$COMPILE
# Run Benchmark with all the commands
$BENCH "${COMMANDS[@]}"
cd ..
fi
done
21 changes: 7 additions & 14 deletions problem-1/problem-1.mojo
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
from python import Python
fn lengthOfLastWord(enterword: String) -> Int:
var count: Int = 0

def lengthOfLastWord(enterword: String):
Copy link
Collaborator

@SohaibBazaz SohaibBazaz May 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm trying to understand why you changed the defining keyword here from "def" to "fn". I mean it worked fine with "def", any reason for that?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fn in mojo allow to use types, which enable the compiler to optimize more effectively.


py = Python.import_module("builtins")

count = 0
for i in range(len(enterword)-1, -1, -1):
for i in range(len(enterword) - 1, -1, -1):
if enterword[i] != " ":
count += 1
elif count > 0:
break

print("Length of the last word:")
print(count)


def main():
input_string = "Hello World"
lengthOfLastWord(input_string)
return count


fn main():
for _ in range(100000):
_ = lengthOfLastWord("Hello World")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You will need to write keep(lengthOfLastWord("Hello World"), otherwise lenghOfLastWord gets DCE'd.

from benchmark import keep

...

15 changes: 8 additions & 7 deletions problem-1/problem-1.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
def lengthOfLastWord(enterword):

def length_of_last_word(enterword):
count = 0

for i in range(len(enterword)-1, -1, -1):
Expand All @@ -8,10 +7,12 @@ def lengthOfLastWord(enterword):
elif count > 0:
break

print("Length of last word", count)
return count


def main():
input_string = "Hello World"
result = lengthOfLastWord(input_string)

main()
[length_of_last_word("Hello World") for _ in range(100000)]


if __name__ == "__main__":
main()
28 changes: 9 additions & 19 deletions problem-2/problem-2.mojo
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,32 +1,22 @@
from python import Python

def checkRecord(attendance_record: String):
py = Python.import_module("builtins")
attendance_record_str = attendance_record

absent_count = 0
late_count = 0
fn checkRecord(attendance_record: String) -> Bool:
var attendance_record_str = attendance_record
var absent_count = 0
var late_count = 0

for i in range(len(attendance_record_str)):

if attendance_record_str[i] == 'A':
if attendance_record_str[i] == "A":
absent_count += 1
late_count = 0
elif attendance_record_str[i] == 'L':
elif attendance_record_str[i] == "L":
late_count += 1
else:
late_count = 0

if late_count >= 3 or absent_count >= 2:
print("The attendance record is not acceptable.")
return False

print("The attendance record is acceptable.")
return True


def main():
checkRecord("PAALP")



fn main():
for _ in range(100000):
_ = checkRecord("PAALP")
20 changes: 9 additions & 11 deletions problem-2/problem-2.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
def checkRecord(attendance_record):

def check_record(attendance_record):
attendance_record_str = str(attendance_record)

absent_count = 0
late_count = 0

for i in range(len(attendance_record_str)):

if attendance_record_str[i] == 'A':
absent_count += 1
late_count = 0
elif attendance_record_str[i] == 'L':
late_count += 1
else:
late_count = 0

if late_count >= 3 or absent_count >= 2:
print("The attendance record is not acceptable.")
# print("The attendance record is not acceptable.")
return False

print("The attendance record is acceptable.")
# print("The attendance record is acceptable.")
return True

# Main function

def main():
checkRecord("PAALP")

main()
[check_record("PPALLP") for _ in range(100000)]


if __name__ == "__main__":
main()
26 changes: 9 additions & 17 deletions problem-3/problem-3.mojo
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
from python import Python
fn makeGood(s: String) -> String:
var result = str("")
var i = 0

def makeGood(s: String):
py = Python.import_module("builtins")
result = str("")

i = 0
while i < len(s):

if i < len(s) - 1 and py.abs(ord(s[i]) - ord(s[i + 1])) == 32:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could see why importing from python was not necessary in my other codes but here this should be working just fine, no?


if i < len(s) - 1 and abs(ord(s[i]) - ord(s[i + 1])) == 32:
i += 2
else:

result = result + s[i]
i += 1

print(result)
return result


def main():

input_str = "leEeetcode"
makeGood(input_str)

#print("After making the string good:", result)
fn main():
for _ in range(1000000):
_ = makeGood("leEeetcode")
17 changes: 7 additions & 10 deletions problem-3/problem-3.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
def makeGood(s):
def make_good(s):
result = ""

i = 0

while i < len(s):

if i < len(s) - 1 and abs(ord(s[i]) - ord(s[i + 1])) == 32:

i += 2
else:

result += s[i]
i += 1

return result


def main():

input_str = "leEeetcode"
output_str = makeGood(input_str)
print(output_str)
[make_good("leEeetcode") for _ in range(1000000)]


main()
if __name__ == "__main__":
main()
36 changes: 11 additions & 25 deletions problem-4/problem-4.mojo
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,36 +1,22 @@
from python import Python
fn detectCapitalUse(word: String) -> Bool:
var uinput = word
var number_of_capital_letters = 0

def detectCapitalUse(word: String):
py = Python.import_module("builtins")

uinput = word

number_of_capital_letters = 0
for i in range(len(uinput)):
letter = uinput[i]
var letter = uinput[i]
if ord(letter) < 97:
number_of_capital_letters += 1

number_of_small_letters = len(uinput) - number_of_capital_letters
var number_of_small_letters = len(uinput) - number_of_capital_letters

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):
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):
return True
else:
return False

def main():
result = detectCapitalUse("USA")
print("Result:")
print(result)












fn main():
for _ in range(1000000):
_ = detectCapitalUse("USA")
21 changes: 11 additions & 10 deletions problem-4/problem-4.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@

def detectCapitalUse(word):


def detect_capital_use(word):
uinput = str(word)

number_of_capital_letters = 0

for i in range(len(uinput)):
letter = uinput[i]
if ord(letter) < 97:
number_of_capital_letters += 1

number_of_small_letters = len(uinput) - number_of_capital_letters

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):
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)):
return True
else:
return False


def main():
result = detectCapitalUse("USA")
print("Result:")
print(result)
main()
[detect_capital_use("USA") for _ in range(1000000)]


if __name__ == "__main__":
main()
17 changes: 8 additions & 9 deletions problem-5/problem-5.mojo
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
from python import Python
fn findTheDifference(s:String, t:String) -> String:
var result = str("")

def findTheDifference(s:String, t:String):
py = Python.import_module("builtins")

for i in range(len(t)):
if s[i] != t[i]:
result = t[i]
print("The difference is:")
print(result)
break

def main():
findTheDifference("abcd", "abced")
return result


fn main():
for _ in range(100000):
_ = findTheDifference("abcd", "abced")
13 changes: 8 additions & 5 deletions problem-5/problem-5.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
def findTheDifference(s, t):

def find_the_difference(s, t):
for i in range(len(t)):
if s[i] != t[i]:
result = t[i]
break

print("The difference is:", result)
return result


def main():
findTheDifference("abcd", "abced")
main()
[find_the_difference("abcd", "abced") for _ in range(100000)]


if __name__ == "__main__":
main()