Is "Mod" fastest method?

BlitzMax Forums/BlitzMax Beginners Area/Is "Mod" fastest method?

Shortwind(Posted 2010) [#1]
Simple question here: In the following code snippet:

Strict

Local a:Int=233

If (a Mod 5)=0 Print "Yes!"



The BlitzMax Compiler produces the following simple ASM code:

mov eax,233
mov ecx,5
cdq
idiv ecx
cmp edx,0
jne _27

Is using "Mod" the fastest method in BlitzMax for testing if some given number is a factor of another?

Thanks :D


Jesse(Posted 2010) [#2]
it seems to be as fast as possible for integers any way. floats might be a different story. I doubt anyone here knows how many ticks do idiv take to execute. You might want to do your research in the MinGW site, Might have better luck there.


ImaginaryHuman(Posted 2010) [#3]
I expect that `idiv` command has to do a division before it can tell you the answer and probably is quite a slow instruction.

If your mod value is a power of 2 you can use masking to make it faster.

If (a & $00000004)=a then Print "Yes"     'test if a is an exact multiple of 16


There might be some kind of trick to do faster modulo's for other numbers. Check the bit twiddling hacks page for example: http://graphics.stanford.edu/~seander/bithacks.html

see "Modulus division (aka computing remainders)"


Dreamora(Posted 2010) [#4]
if you are worried about the performance of math code, alter bmk and add the -ffast-math flag to the compilation and recompile all modules.

Will give you a nice boost but cost you float precision (but if you go for bit twiddling and similar "opt for precision sacrifice" optimizations you will likely not worry about that :)


Jesse(Posted 2010) [#5]
you should complete a game first and do your research based on that. You will find out that the little things you have been most concern about generally will be the least of your problems and will not make much difference. That is why most good programmers merely concern with profiling and optimizing the most stressful parts of their games only.


Shortwind(Posted 2010) [#6]
Thanks for all the feedback, it's much appreciated. :D

It will probably seem inherantly silly to some, but I don't do any actual game programming in BlitzMax... At least not yet. I am primarily using BlitzMax to test ideas, and hone my amature programming knowledge.

Jesse, in the past you've been instrumental in helping me overcome some initial brain farts, and in helping me understand how to better organize some of my coding practices. In that I am very gratefull! :D

Over-all I've been very pleased with the responses I've recieved from some of my silly (and sometimes serious) questions and problems. I believe, in general, this is the reason why I like BlitzMax and the community. The community actually makes the programming language more tolerable. So many other programming forums I've tried to use are filled with egotistical, pompus blowhards intent on only one thing, making you feel supremely inferrior, worthless, and just plain to stupid to ever be able to program in "their real" programming language.

It certainly won't appear this way to most, but I actually started programming back in the first part of 1982 on an Apple II. I've also worked on the old TI-99/4a. But my first real work, and where I actually learned the most, was on the old Commodore 64. With the help of a school mate, and a highschool teacher, I did extensive work in basic, and most importantly (to me anyway), 6502/10 ASM programming. Back in those days, memory was slim, and the concern over super compact, and optimized coding was the highest priority. I suppose this is where I get too bogged down in worrying about optimization. Back then it was fun, and good learning, to compete with classmates, and the teacher, on finding the fastest, most effiecent method for programming some routine or function.

As far as higher level languages go, I actually loved Turbo Pascal 3, mixed with some simple 8088 asm routines. That was challenging and fun. As we all should know, BASIC itself has been taking a bad rap ever since it was first invented. This has always basically annoyed me to no end. For years I could never figure out why all these so called great programmers could not get their act together and create a "real" compiler for basic that would rival all the higher level languages (pascal, c, fortran, etc.) It always seemed like they were all being lazy, and simply did not want this so called "toy" language to ever be a real threat to its' bigger, and not necessarily, better brothers.

To this day, I'd say that as far as basic dialects and compilers go, BlitzMax is probably the fastest (at what it does best.) But it still is beaten out by C... Why? My knowledge of compiler construction is very limited, but I still can not figure out why this obfiscated language of C is still beating the pants off anything else out there... I wish I was super smart and could pour over the c libs and compiler code to understand what went wrong. :D But alas, I'll never be that smart, or intuitive enough to write my own compiler to put C to shame. LOL

Basically I am greatful to all who have helped me, and you can be assured I'll be posting more silly and serious questions to the forums.

Thanks,
:D


Jesse(Posted 2010) [#7]
the problems you mentioned are not unique to BMax. it's clearly visible in c and pascal and every other high level language is missing simple instructions like those. I used to program the 8086 processor so I know a lot of optimization binary hack code to make my program run faster but most of the time it is not as important and does tend to make it hard for others to understand.

I too have been programing sense 1982. Funny but my first experience was with Apple II also. My first personal computer was that commodore vic20. later the Trs80, and the Tandy 1000 was my first real PC. I have experience in Masm, Cobol, Fortran, Pascal, c, qbasic and Bmax. I have learned what I know of OO programming on my own with Bmax.

based on all of these experiences, all that I can tell is that if you want to produce the fastest code is to stick with Assembler. even then, I am sure you know that no matter how good you think your code is, there is always going to be someone who can do it with fewer instructions and faster.
The era of creative computers is just a shadow of what it once was. all that is left is a bunch of lazy programmers that rely on someone else's lazy code to get anywhere. That was directed to me as well. Not that it's a bad thing anyway. The reason is that there is so much technology out there that most of us could not catch up to it even if we were the smartest programmers in the world.

So, if those little things are what drives you, do stick wit what you are doing. After all, most of us here are just hobbyist programmers anyway. We are doing what we want not what some greedy corporation is telling us to do which is where the fun ends.
But if your goal is to make a game or an application for commercial purpose then do follow the "efficiency model" and soon you will see that all those little details will stop becoming important, most of the time anyway.


Xerra(Posted 2010) [#8]
I think a lot of people using Blitzmax came from the 8 bit school of programming. Blitz was originally an Amiga product that Mr Sibly came up with as well as writing some pretty good games back in the early 90's. Of course the Amiga was a 16 bit machine but I reckon 90% of C64 users back then upgraded to them because they were such wonderful machines and had the Commodore branding.

I cut my teeth writing basic programs on the Vic 20 and C64 where I was forced to use assembly for some parts in game that required fast moving characters and scrolling routines for example. I wasn't much good at it as there wasn't a compiler system back then and most assemblers were pretty basic to say the least as well.

Using Blitz is a luxury if you still think of programming in the same way you did back then but, like it's been illustrated already, you really don't have to so much now. It's always a good idea to have readable, tidy code which isn't sloppy in how it does things because you're not worried about it running too slow, sure. But mostly you're not going to run out of juice on any kind of indie game you write because it's probably quite tricky to write code that bad with an OOP system like Blitz.

Programming has kind of evolved - and it really had to. Nobody wants to write code like

LDA #255
STA 53280
NOP
NOP
RTS

Or Poke 53280,255 but that's all we had at the time. If we were still trying to work using a language like that then most of us would have long gone to move onto a more sane hobby.


Wiebo(Posted 2010) [#9]
STA $D020, but I forgive you!


Xerra(Posted 2010) [#10]
Hahah oh yeah, it was all hex in those days :)


Nate the Great(Posted 2010) [#11]
but I forgive you!



unfortunately computers do not have a soul and therefore do not have the ability to forgive