bennyboy Wrote:This isn't something we have to philosophize on-- go ahead and make a simple switch statement and check the assembled code.Do you happen to know how to do that with JavaScript? I know JavaScript is, in modern engines, first compiled to a superset of WebAssembly, but I have no idea how to see that WebAssembly, and I also don't know how to see the assembly code that WebAssembly is compiled to.
bennyboy Wrote:Anyway, your js code is calculating [codebyte] again and again in the conditional check of every single "if" statement, which is not the same as what you have in the above example.The compiler probably optimizes that away, right? And even if it doesn't, the "and" operation takes a very small amount of time.
bennyboy Wrote:Does it say why?Allegedly, using switch-case is a sign you do not have enough subclasses. If a switch-case is used again and again in methods of some class (in different methods), it's a sign that by which the switch-case branches are logically different classes.
HappySkeptic Wrote:A map that calls different pieces of code.You mean, like virtual methods?
bennyboy Wrote:More readable code can reduce mistakes, and allow other programmers to understand it quickly: a potential savings of hours of real-life human time instead of a few milliseconds of CPU time.In our "Razvoj Programske Podrške po Objektno Orijentiranim Načelima" classes, we were also taught clean and idiomatic code usually results in a faster compiled program, because the compiler is optimized to work with idiomatic code.
bennyboy Wrote:I'm assuming you're using JavaScript because this is meant to run online. Did I miss (or forget) a link to a webpage that is actually running this? Do you have sample code that you feel is executing too slowly?It is on my website. The example code that executes unacceptably slow is my "Decimal to Binary" example program:
Code:
;WARNING: I have tried to run this on
; real PicoBlaze, but failed.
; Now, I did not have time to
; test whether a simpler
; example program using UART
; would work, to see if it
; is a problem with this
; program, or if it is maybe
; a problem with the way I
; set up PicoBlaze. I have
; opened a
; StackOverflow question
; about it, but, thus far, I
; received no response.
;This is an example program that uses
;UART, the interface that PicoBlaze uses
;for connecting to terminals (a DOS-like
;user interface, with a keyboard and a
;screen capable of displaying text).
;It loads base-10 integer numbers from
;the terminal, converts them into binary,
;and then prints the binary
;representations back onto the terminal.
;Example input would be:
;1
;2
;4
;8
;15
;127
;255
;
;And the expected output is:
;1_(10)=1_(2)
;2_(10)=10_(2)
;4_(10)=100_(2)
;8_(10)=1000_(2)
;15_(10)=1111_(2)
;127_(10)=1111111_(2)
;255_(10)=11111111_(2)
;
;Note that you need to click the
;"Enable UART" button in order to use it.
;Also, the trailing empty line in the
;input is necessary for the result to be
;printed.
;Now follows some boilerplate code
;we use in our Computer Architecture
;classes...
CONSTANT LED_PORT, 00
CONSTANT HEX1_PORT, 01
CONSTANT HEX2_PORT, 02
CONSTANT UART_TX_PORT, 03
CONSTANT UART_RESET_PORT, 04
CONSTANT SW_PORT, 00
CONSTANT BTN_PORT, 01
CONSTANT UART_STATUS_PORT, 02
CONSTANT UART_RX_PORT, 03
; Tx data_present
CONSTANT U_TX_D, 00000001'b
; Tx FIFO half_full
CONSTANT U_TX_H, 00000010'b
; TxFIFO full
CONSTANT U_TX_F, 00000100'b
; Rxdata_present
CONSTANT U_RX_D, 00001000'b
; RxFIFO half_full
CONSTANT U_RX_H, 00010000'b
; RxFIFO full
CONSTANT U_RX_F, 00100000'b
ADDRESS 000
START:
;At the beginning, the number is 0.
load s0, 0
;And we are storing its string
;representation at the beginning
;of RAM.
namereg s3, pointer
load pointer, 0
;Now follows a loop to load
;the digits of the number.
loading_the_number:
;Load a character from the UART
;terminal.
call UART_RX
;Check whether the character is a digit.
compare s9, "0"
;If it is not a digit, jump to the
;part of the program for printing
;the number you have got.
jump c,print_the_number
compare s9, "9" + 1
;Suggestion by a StackExchange user.
jump nc, print_the_number
;If it is a digit, store it into RAM.
store s9, (pointer)
add pointer, 1
;Multiply the number you have got by 10.
load sf, s0
call multiply_by_10
load s0, se
;Then, convert the digit from ASCII
;into binary.
sub s9, "0"
;And then add it to the number you
;have got.
add s0, s9
call c, abort ;In case of overflow.
jump loading_the_number ;Repeat until a
;non-digit is
;loaded.
print_the_number:
;If there are no digits to be printed,
;do not print anything.
sub pointer, 0
jump z, START
print_the_decimal:
load s4, pointer
load pointer, 0
printing_the_decimal_loop:
compare pointer, s4
jump nc, end_of_printing_the_decimal
fetch s9, (pointer)
;Do some basic sanity check: Is the
;character you are printing indeed
;a decimal digit?
compare s9, "0"
call c , abort
compare s9, "9" + 1
call nc, abort
;If it is indeed a decimal digit,
;print it.
call UART_TX
add pointer,1
jump printing_the_decimal_loop
end_of_printing_the_decimal:
;After you have repeated the decimal
;number, print the string "_(10)=".
load s9, "_"
call UART_TX
load s9, "("
call UART_TX
load s9, "1"
call UART_TX
load s9, "0"
call UART_TX
load s9, ")"
call UART_TX
load s9, "="
call UART_TX
;If the number to be printed is
;equal to zero, print 0.
sub s0, 0
jump nz, print_the_binary
load s9, "0"
call UART_TX
jump end_of_printing_loop
print_the_binary:
;Make the pointer point to the
;beginning of RAM.
load pointer, 0
;Now goes a loop which stores the binary
;representation of the number we have
;got into RAM, but reversed.
beginning_of_converting_to_binary:
sub s0, 0
jump z , end_of_converting_to_binary
load s9, "0"
sr0 s0
jump nc, store_digit_to_memory
add s9, 1
store_digit_to_memory:
store s9, (pointer)
add pointer, 1
jump beginning_of_converting_to_binary
end_of_converting_to_binary:
;Do some basic sanity check, such as that
;the pointer does not point to zero.
compare pointer, 0
call z, abort ;Something went wrong
;so end the program.
;Check whether there are more than 8 bits.
compare pointer,9
call nc, abort
;Now goes a loop which will print
;the binary number in RAM, with digits
;in the correct order. The pointer now
;points at a memory location right after
;the binary number (not at the last digit,
;but after it).
beginning_of_printing_loop:
sub pointer, 1
jump c , end_of_printing_loop
fetch s9 , (pointer)
;Do some basic sanity check:
;Is the character the pointer points to
;indeed a binary digit?
compare s9, "0"
jump z , memory_is_fine
compare s9, "1"
jump z , memory_is_fine
call abort ;Something went wrong,
;so end the program.
memory_is_fine:
;If everything is fine, print that
;digit.
call UART_TX
;Repeat until you have printed all
;digits of the binary number
;stored in RAM.
jump beginning_of_printing_loop
end_of_printing_loop:
;After you have printed that binary
;number, print the string "_(2)" and
;a new-line.
load s9, "_"
call UART_TX
load s9, "("
call UART_TX
load s9, "2"
call UART_TX
load s9, ")"
call UART_TX
load s9, a ;newline character, 0xa=='\n'.
call UART_TX
;The program runs in an infinite loop...
JUMP START
multiply_by_10:
load se, sf
add se, se
call c , abort
add se, se
call c , abort
add se, sf
call c , abort
add se, se
call c , abort
return
abort:
load s9, "E"
call UART_TX
load s9, "R"
call UART_TX
load s9, "R"
call UART_TX
load s9, "O"
call UART_TX
load s9, "R"
call UART_TX
load s9, "!"
call UART_TX
load s9, a ;newline
call UART_TX
infinite_loop:
jump infinite_loop
return
;Now follows some boilerplate code
;we use in our Computer Architecture
;classes...
UART_RX:
INPUT sA, UART_STATUS_PORT
TEST sA, U_RX_D
JUMP Z , UART_RX
INPUT s9, UART_RX_PORT
RETURN
UART_TX:
INPUT sA, UART_STATUS_PORT
TEST sA, U_TX_F
JUMP NZ, UART_TX
OUTPUT s9, UART_TX_PORT
RETURN