Testing efficiency
Asked Answered
T

8

0

So, is there a way to test efficiency of your code, or is it just over time you kind of learn what is more better for CPU and RAM usage and the likes.

An example I have two ways I have written this code below, is there a way to determine which is better?

	if theUnit.active == true:
		for button in buttons:
			button.visible = true
	else:
		for button in buttons:
			button.visible = false
#the other way I wrote it was 
	if theUnit.active == true:
		accel.visible = true
		decel.visible = true
                turn_l.visible = true
                turn_r.visible = true
	else:
		accel.visible = false
		decel.visible = false
                turn_l.visible = false
                turn_r.visible = false

So the for loop uses buttons as an array that holds the four references to the buttons, so that is an extra variable that is used with that method. Not sure if one is superior over the other. Or how to figure out a way to determining that.

Tradesman answered 17/9, 2023 at 17:54 Comment(0)
T
1

Tradesman The first one is greatly superior because it's much shorter, and has less repetition. This makes it much more readable and easier to follow.

Always strive to avoid repetition when writing code. A lot of repetition is a sure way to introduce bugs and you have more work to do when making changes. Short code tends to be self explanatory and more expressive.

You needn't worry about performance just yet. But if you want to test how performant the code is, the best way is to use built-in code profiler.

That said the first example should be made even shorter:

for button in buttons:
	button.visible = theUnit.active
Tartary answered 17/9, 2023 at 18:4 Comment(0)
T
0

Tartary Also if you are using a variable that isn't used in any other function, and it also isn't being changed, is there a rule about if you make it a global or local variable?

Awesome, appreciate it mate. I am definitely in the pragmatic stage with my learning how to code, I tend towards if it works good, but I also want to stretch my thinking in how to streamline and simplify the code to make it less bulky.

That simplification is very smart, now it will just take the bool from theUnit.active eliminating the if/else statement removing the need to check as many conditions.

Tradesman answered 17/9, 2023 at 19:0 Comment(0)
T
1

Tradesman You should always make variables as local as you can, Give them the smallest possible scope. There are exceptions but that's for another topic.

To make your code more expressive, always organize it in functions that do small, self-contained amount of work that can be neatly named. So above example can be made even better if you define it as a function whose name clearly tells us exactly what it does. Now whenever your code needs to do that thing, it'll do it in one line of self-explanatory code.

func set_button_visibility(visible: bool):
	for button in buttons:
		button.visible = visible

And then call it whenever you need to change the buttons visibility state:

set_button_visibility(theUnit.active)
Tartary answered 17/9, 2023 at 19:43 Comment(0)
I
1

Nitpick:

For more clarity, rename the function:

func set_buttons_visible(visible: bool):

or:

func show_buttons(visible: bool):

Interlink answered 17/9, 2023 at 20:11 Comment(0)
T
0

Tartary So is it better in general to have more functions? I tend towards being function adverse, is it better to break functions into smaller functions and just call those functions? I guess that'd make my code easier to search.

Tradesman answered 17/9, 2023 at 20:47 Comment(0)
I
1

Tradesman is it better to break functions into smaller functions

It makes the code easier to read and maintain, as long as the function's purpose is clear.

Interlink answered 17/9, 2023 at 20:59 Comment(0)
T
1

Tradesman It's best to have optimal number of functions. Breaking the code into too many too small functions isn't good either. The purpose of functions is to organize your code into neat logical units that can be reused. If you see that you have written the same or similar block of code more than once, it's likely a candidate for a function.

Tartary answered 17/9, 2023 at 21:2 Comment(0)
T
0

Tartary So to get an idea if I am going overboard the opposite direction, here is some code that I tried to move into multiple functions. Before all this code was in a single func.

func move_turn():
	var sortNum = 0
	#Sorts the ships based on speed in order to call turn order
	if sortNum == turnMove:
		arrShip.sort_custom(func(a, b): return a.speed<b.speed)
		sortNum += 1
	if theUnit.turnCount == turnMove:
		ship_cycle()
	else:
		end_move_turn()
	show_buttons(theUnit.active)
func ship_cycle():
	#Activating units to move
	if theUnit.active == false:
		theUnit.active = true
		#Finishing unit turn, or prematurely ending ship turn and resetting it
	if theUnit.thrust <= 0 or Input.is_action_just_pressed("action_complete"):
		theUnit.turnCount += 1
		arrShip.push_back(theUnit)
		arrShip.remove_at(i)
		theUnit = arrShip[i]
func end_move_turn():
	turnMove += 1
	for ship in arrShip:
		ship.thrust = ship.maxThrust
		ship.active = false
func show_buttons(shown : bool):
	var buttons = [accel,decel,turn_l,turn_r]
	for button in buttons:
		button.visible = shown

Is this going overboard with functions, or do you think this is along the lines of what you were thinking, I am just trying to get a picture, I had this all consolidated into the single move function, along with the code that I gave as an example before.

Tradesman answered 17/9, 2023 at 21:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.