-
Notifications
You must be signed in to change notification settings - Fork 2
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
base: main
Are you sure you want to change the base?
Changes from all commits
01f8fe7
3041b4a
a055046
28b3cac
1777b63
99df50c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.bin |
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 |
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): | ||
|
||
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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You will need to write from benchmark import keep
... |
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") |
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() |
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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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") |
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() |
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") |
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() |
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") |
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() |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.