123
-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 0|645|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=-
Socoder -> C/C++/C#/Other -> C++ If this or that?

Mon, 20 Aug 2018, 11:42
Jayenkai

C++ If this or that?


Sitting here coding lots of stuff for JNKPlat2018, simple tile objects and whatnot.

Have the line..
if (fn==1 || fn==2 || fn==3 || fn==125) {return 1;}
..
Works fine, but I know that requires 4 lookups of fn.. Just wondering, if anyone knows, is there a if fn==(1 or 2 or 3 or 125)
I know that literally typing if fn==(1 or 2 or 3 or 125) won't work, because that would essentially be if fn==(127)..
But is there something that might do that?!
*shrugs*

Still learning!!

-=-=-
''Load, Next List!''
Mon, 20 Aug 2018, 11:56
cyangames


Might reduce the number of calls overall?

-=-=-
Web / Game Dev, occasionally finishes off coding games also!
Mon, 20 Aug 2018, 12:13
Jayenkai
Oh yeah.. I think c also does multi case (eg case 1,2,3,4,5) too, doesn't it?
That's probably a slightly quicker way to get it to work.
Ta.

-=-=-
''Load, Next List!''
Mon, 20 Aug 2018, 12:24
cyangames
GBDK C does and I think PHP is similar so it (should be) the same syntax (hopefully!)

-=-=-
Web / Game Dev, occasionally finishes off coding games also!
Mon, 20 Aug 2018, 16:01
Retro
If the range of case values is limited, using an array lookup may be faster than a switch statement.
Mon, 20 Aug 2018, 16:32
Jayenkai
Possibly, but I’d need half a dozen arrays for this purpose, and.. quite frankly, things are getting alarmingly complicated with the level editor!!
3DS memory is screaming for breath underneath the mountains of insanity

-=-=-
''Load, Next List!''
Mon, 20 Aug 2018, 17:57
TomToad
I believe that conditionals in C++ are short circuiting. The right side of the || will not evaluate if the left side is true.

produces this output

foo 1
foo 2
Match

In Visual Studio 2017,
Tue, 21 Aug 2018, 00:57
cyangames
I guess it all depends on the compiler really, each method should reduce down to roughly the same in assembly

-=-=-
Web / Game Dev, occasionally finishes off coding games also!
Tue, 21 Aug 2018, 03:01
Krakatomato
"Works fine, but I know that requires 4 lookups of fn.. "

No - it will short-circuit as Tom stated above. No point trying to optimise that to something else as you're already in highly-optimised assembly land.
Tue, 21 Aug 2018, 03:51
cyangames
Aye. In NESASM you'd end up with something along the lines of.



Where scriptToRun is a subroutine to JUMP to in memory

If it's a dumb compiler then it would load a register for each conditional but we're talking about a tiny number of CPU cycles difference here.

-=-=-
Web / Game Dev, occasionally finishes off coding games also!
Tue, 21 Aug 2018, 04:02
Jayenkai
For those claiming “don’t worry, it'll short circuit”
... sure, if it's the first value..

But if it’s the 18th value, then that’s 18 checks that WILL be happening.
And if all 32x32 checks are the 18th, then that’s over 18,000 events..
Soon adds up!!

-=-=-
''Load, Next List!''
Tue, 21 Aug 2018, 04:05
Krakatomato
You're really looking in the wrong place if you're hoping to improve performance. And using a switch statement would result in essentially the same thing - the asm equivalent of a whole bunch of IF statements - 18 of them.
Tue, 21 Aug 2018, 04:12
Jayenkai
I'm not "worried".. I was just wondering if there was, perhaps, a slightly better way.
I guess the answer is, not!

Oh, and the switch->if thing.. Don't ever peek into how Monkey-X converts Switches for Android!!

-=-=-
''Load, Next List!''
Tue, 21 Aug 2018, 05:15
cyangames
If the numbers are easily groupable by just a greater then / less than / mod function then you'll end up with smaller code.

Failing that, order the checks by percieved popularity?

-=-=-
Web / Game Dev, occasionally finishes off coding games also!
Tue, 21 Aug 2018, 11:14
TomToad
If you need to test against such a large list of values, it might be better to use sets as they use a binary search. The maximum number of tests would be logâ‚‚n. So 18 values would require at most 5 tests. 100 values would require at most 7 tests.


Edit: actually didn't need to create an iterator since I am only checking the presence of the value. Don't need the actual value returned.
Edit again: Apparently set.count() is more efficient than set.find() if you don't need to use the returned value.
Tue, 21 Aug 2018, 12:43
Krakatomato
^^^ that
Tue, 21 Aug 2018, 13:57
Jayenkai
Seems viable, and quick tests appear to work on Windows and 3DS targets. Will have to double-check in Linux and Mac. Mac's, so far, being the bitchiest of all the compilers.

-=-=-
''Load, Next List!''
Tue, 21 Aug 2018, 14:49
Krakatomato
Tbh, you're spending too much time looking into something that saves practically sod all. Honestly, from almost 25 years in the industry, and working on extremely low level devices with almost no memory whatsoever, I know you're really looking in the wrong place to optimise.

If you're suffering problems with performance then break it down. Can you profile it? If not, start to reduce what you think are the problem areas.
Tue, 21 Aug 2018, 15:00
Jayenkai
Drawing a new tile on the level editor of JNKPlat2018 results in the editor scanning a whole line of the level to ensure the checksum's valid at the end of the line.
This requires 40 lookups, plus maths for the checksum which includes checks for tile types as well as the tile value itself.
It's weird, but doing that on the 3DS is damn near realtime, whilst the Windows version is running slightly slower.. There's a noticeable stutter as it does it.

That's why I wanted to see if there was a quicker way to check all those values.
It's nothing major. I just thought it was odd that the PC version was running slower than the 3DS one, when the 3DS is running at only 233mhz!!!

Plus, hey, optimisation never hurt anyone!

-=-=-
''Load, Next List!''