• Status: Solved
• Priority: Medium
• Security: Public
• Views: 288

# Which coding is better?

ok, here's my dillema.  Now, you probably realize as I do that VB is well, really, really slow.  I mean, I know C++ but, it just isn't as practical for some things.

Anyway, I was wondering which of the ways of coding this would actually be faster, the use of variables way or the straight out number crunching:

This is just a simple RGB to Dec To RGB conversion thing, it is meant to be hard to read.

Straight Numbers:

MsgBox "RGB(" & (RGB(98, 64, 128) - ((Int((RGB(98, 64, 128) - (Int(RGB(98, 64, 128) / 65536) * 65536)) / 256) * 256) + (Int(RGB(98, 64, 128) / 65536) * 65536))) & ", " & Int((RGB(98, 64, 128) - (Int(RGB(98, 64, 128) / 65536) * 65536)) / 256) & ", " & Int(RGB(98, 64, 128) / 65536) & ")"

yes, it is all one line.

Or, we can use the variable way:

A = RGB(98, 64, 128)
B = Int(A / 65536)
G = Int((A - (Int(A / 65536) * 65536)) / 256)
R = A - ((G * 256) + (B * 65536))
MsgBox "RGB(" & R & ", " & G & ", " & B & ")"

The variable way is of course much easier to read but, I don't write my code to be read by other programmers so, that doesn't need to be taken into consideration.

Anyway, see the thing is, I don't care about code size or readability or anything, I just want max performance, which way would be better and/or faster?
0
Corpse
• 7
• 5
• 4
• +3
1 Solution

Billing EngineerCommented:
The second ways should be faster, as you are reusing results of computation. the processor later would in any case need several cycles to do the calculations, so prefer the 2nd solution.
By the way, you know the mod(ulo) operator in VB?
0

Author Commented:
hmm.. I thought the second way would be faster, I guess variables aren't that bad unless they are overused.
0

Billing EngineerCommented:
Somewhere memory will be used, even if you don't explicitely declare variables for it. The compiler hides much of the work in this case you should do in most of the cases.
0

Commented:
I think 2 nd option will be useful because the system has the oppurtunity of parsing the entire statement. May be first time it will be somewhat slow but from next time on, u can feel the speed of the execution. This is the logic which is effectively used in PreparedStatement in ADO. So, second option is optimum and effective in every way.
0

Commented:
CalculatingYourValuesAndThenConcatenatingShortStringsIsMuchFaster,RatherThanCalculatingAndConcateningThemAllAtOnce.. <---- Screw legibility.. pure peformance!!!! LOL.

Also, as you are working with integers, change your division /'s to \'s (backslashes), avoiding a needless call to the math copressor.

Oh.. and by the way.. fully loaded 650mhz screamers now sell for \$999 from Gateway.. We in the VB world are doing just fine.. <wink>.
0

Commented:
You think it's a dilemma? How many convertions do you REALLY need in your program?
OK, let's test it :

Public Sub Main()
Dim s As Long, i As Long, Rstring\$
Dim A As Long, B%, G%, R%

s = Timer
For i = 1 To K
Rstring = "RGB(" & (RGB(98, 64, 128) - ((Int((RGB(98, 64, 128) - (Int(RGB(98, 64, 128) / 65536) * 65536)) / 256) * 256) + (Int(RGB(98, 64, 128) / 65536) * 65536))) & ", " & Int((RGB(98, 64, 128) - (Int(RGB(98, 64, 128) / 65536) * 65536)) / 256) & ", " & Int(RGB(98, 64, 128) / 65536) & ")"
Next
Debug.Print Timer - s

s = Timer
For i = 1 To K
A = RGB(98, 64, 128)
B = Int(A / 65536)
G = Int((A - (Int(A / 65536) * 65536)) / 256)
R = A - ((G * 256) + (B * 65536))
Rstring = "RGB(" & R & ", " & G & ", " & B & ")"

Next
Debug.Print Timer - s

End Sub

Hmm, interestig results....
K=1000000
20.6328125
12.55078125
K=100000
2.4375
1.73046875
K=10000
0.4453125
0.56640625
K=1000
0.17578125
0.1953125

0

Billing EngineerCommented:
Interesting enough, but did you do average timing (execute your code 1000 times)? For single statements, you will rarely increase performance drastically, but you should not start using dirty code (ask the pro's)

I used VB6 with output to a text control, and i even realized negative times (1 execution), so you certainly have a very, very, very old machine (-:

K=1
-0.248999999999999
-0.239000000000001
K=10
-0.239000000000001
-0.239000000000001
K=100
-0.239000000000001
-0.228999999999999
K=1000
-0.189
-0.158999999999999
K=10000
0.260999999999999
0.532
K=100000
3.778
2.532
0

Author Commented:
wsh2 :  About that legibility thing.  Once my code is pretty much bug free and all what reason do I have to read it easily?

samopal : I oddly enough never would have thought of actually timing it and seeing how long it takes.  Now I can actually see that the second way is always faster.
0

Commented:
Corpse:
Never say Never.. in NeverNever Land.. <smile>. The strength of any developer, is in the reusability of their code. Trust me.. there will be a day when you need to go back.. and beg, borrow or steal an archive. When that happens, I think you will find (like I have many times.. <sheesh>).. that half your time is spent scratching your head, swearing, and uttering "Now.. what the hell did I do here"... <lol>. "Genius At Work" signs are expensive, legible and uncluttered code is free.. <smile>.

A word to my fellow VB Novices and Experts.. Wanna have fun?.. Save your first programming work of art!!! 2 years later, go back and read it (if you can).. you will absolutely roar be amazed and most likely roar in glee.. <smile>.

Corpse, Please know, that no offense was intended. It's just that I have programmed, and managed programmers, for way to long.. <adjusting his pocket protector>. Don't think for a second, that I haven't been down the same road.. and with the same verve as yours.. <smile>. I guess its lucky that from all the head scratching I've done, I still have hair.. <lol>.

See ya in two years.. <wink>.
0

Commented:
wsh2,

<LOL!>  Thanks for inviting me to take a look at this!

Guys...

I only have a couple of things to say...

There is absolutely no correlation between making your source code unreadable like this and having fast executing code.  It just makes the human reading it frustrated.  Once the program is compiled, the machine language instructions are being executed... Not the source code.

2) Document, Document, Document... wsh2 is right. If a piece of code is well documented, and you can read it, it's really easy to clip pieces you've written into new programs.

3) When you are trying to invision which piece of code is faster, "play computer".  I mean, visualize the instructions that are actually being executed.

For example:

RGB(98, 64, 128)

returns a long integer based on the calculation of 3 arguments.  Just "playing computer", to come up with this long integer takes several steps.  What is it really doing?

* Jumping to an executable subroutine
* Allocating an area in memory
* Doing hex math to put the 98 in the least significant bits of the allocated memory
* Doing hex math to put the 64 in the next least significant bits of the allocated memory
* doing the hex math to put the 128 in the most significant bits of the allocated memory
* returning from the subroutine

In the expression RGB(98, 64, 128), 98 is x62, 64 is x40 and 128 is x80... and when you look at the number that is returned from the function, you get the long number 8405090, which, in hex is (guess what?)

x804062

By looking at the pseudo-steps we went through to get this number 8405090, I count 6 things to do... therefore, when you do:

RGB(98, 64, 128) - RGB(98, 64, 128)...

as part of the original code shown, I see 13 things that have to happen... 6 for each RGB function and 1 to subtract the two results.

A = RGB(98, 64, 128)...
A = A - A...

I only see 8 thing to do... Since the computer is clock based, it does 8 instructions faster than 13...

But then why do 8 things when you can do:

A = 8405090 - 8405090 ....

Ok, I'm getting down off my soapbox now...

0

Commented:
So did everyone here die or what??  Anybody home?

0

Commented:
I mean I AM talking to a Corpse, but am I REALLY talking to a corpse?? <G>
0

Author Commented:
you are talking to a corpse..
0

Commented:
Corpse?.. <ears perking up>.. Al Gore.. is that You?.. <smile>
0

Author Commented:
I meant to post something the other day but, I had typed it up after chugging a fifth of the all wonderful Southern Comfort.  Anyway, I can see the major difference in speed when it is actually timed (using samopal's code).  It seems odd though that it takes less calculation time to set and use a variable than to just crunch some numbers.

I mean, think about it, it should take a lot less time to do extra division than to access variables and store them.  Of course, it's probably more or less the other functions like Int(), RGB() and everything else that slowed it down.

I think I'm just gonna go straight out C++ from now on, VB is just way too slow.
0

Commented:
So you're not going to award the points at all??
0

Author Commented:
hey, I'll award them to whoever wants them.  Someone just needs to post as an answer.

After someone posts as an answer though I'll wait about a day to make sure there are no objections to them getting the points.
0

Billing EngineerCommented:
First: You can accept a comment as an answer (as far as I know)
Second: Straight coding can be better, if the compiler "sees" the best way, otherwise use C++ to optimize in an ever-harder-way-to-read
0

Commented:
Yea, Just pick the best comment and award the points... Give'm a good grade too. ;-)

0

Author Commented:
Adjusted points from 50 to 100
0

Author Commented:
I'm not saying that everyone else didn't help too but, this comment got me thinking cause it actually timed how long it took to do out the commands.  I wouldn't have though of doing that on my own..
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

## Featured Post

• 7
• 5
• 4
• +3
Tackle projects and never again get stuck behind a technical roadblock.