PDA

View Full Version : If Else More Efficiently


Fuego
04-13-2004, 04:23 PM
Can I write this more efficiently?


While Not rsResultaat.EOF
If rsResultaat("IN_DAG") = True Then
bolDA = "x"
Else
bolDA = ""
End If
If rsResultaat("IN_TOP") = True Then
bolTT = "x"
Else
bolTT = ""
End If
If rsResultaat("IN_LND") <> 0 Then
bolLA = "x"
Else
bolLA = ""
End If
If rsResultaat("IN_STR") <> 0 Then
bolSA = "x"
Else
bolSA = ""
End If
If rsResultaat("IN_PLT") <> 0 Then
bolPA = "x"
Else
bolPA = ""
End If
If rsResultaat("IN_NWB") = True Then
bolNB = "x"
Else
bolNB = ""
End If

ghell
04-13-2004, 04:37 PM
all variables start as "" so if they have defaintely not already been set you could just change to
If rsResultaat("IN_DAG") = True Then bolDA = "x"
for example

raf
04-13-2004, 04:39 PM
I don't think so.

Which doesn't mean of course that your overall code couldn't be optimised so that it's a bit more dynamic --> which will probably make it a tiny bit slower, but easier rto maintain and expand.

Depending on further use of this recordset, and the db-format that you use, you could also do this conversion inside your sql-statement and use aliases for each column, so that the recordsetvariables already have the right value and name, and then you could use a getrows to dump the recordset into an array, but this all wount make a lott of differnce.
If it's a large recordset (especialy if it has a high number of records) then you could use a getrows and immedeately clode the recordset and connection and set them to Nothing. On a high load site, this can improve performance conciderably.
<edit>posts crossed</edit>

M@rco
04-14-2004, 02:33 AM
UPDATE: My solution below is NOT identical in function to the code you originally posted. I have assumed that only one of these tests can be true at a time. If this assumption is false then this is NOT an improvement.

Can I write this more efficiently?From a purely code-rearranging/maintenance perspective, then this is probably the optimal form:While Not rsResultaat.EOF

bolDA = ""
bolTT = ""
bolLA = ""
bolSA = ""
bolPA = ""
bolNB = ""

Select Case True
Case rsResultaat("IN_DAG")
bolDA = "x"

Case rsResultaat("IN_TOP")
bolTT = "x"

Case rsResultaat("IN_LND") <> 0
bolLA = "x"

Case rsResultaat("IN_STR") <> 0
bolSA = "x"

Case rsResultaat("IN_PLT") <> 0
bolPA = "x"

Case rsResultaat("IN_NWB")
bolNB = "x"

End SelectHowever, like Raf, I suspect that this could (and should) be avoided entirely by recoding your recordset-populating statement/SP.

Roy Sinclair
04-14-2004, 10:36 PM
A performance improvement is available here, instead of using the construct "rsResultaat("IN_DAG")" you can code it as "rsResultaat.fields("IN_DAG").value". By using the fully qualified name you remove the need to check all the collections within the recordset object for all objects named "IN_DIAG". The ADO code doesn't know that there's only one object named "IN_DIAG" and that it's in the fields collection and it doesn't assume that it's going the be getting the value of the field either, the additional checks you eliminate can indeed save overhead.

M@rco
04-15-2004, 12:23 AM
Well, if we're getting clever...


If you know the numeric order of the fields in the recordset then you could improve Roy's suggestion by accessing the field by index, e.g. rsResultaat.fields(2).value

Even better, you can resolve the field just the once by binding each field object to a variable outside the loop:Set IN_DAG = rsResultaat.Fields("IN_DAG")

While Not rsResultaat.EOF

bolDA = ""
bolTT = ""
bolLA = ""
bolSA = ""
bolPA = ""
bolNB = ""

Select Case True
Case IN_DAG.Value
bolDA = "x"
As you move to the next record in a recordset, the field object stays the same, it's just the value-related properties which are populated, and so the bound variable (which is merely a reference to the field object) reflects this.
;)

But as I said before, this isn't the block of code that needs fiddling with - this logic should be performed on the database end with CASE (or equivalent) statements (assuming your DB supports them).

PS - Fuego, have you been reading these posts?
PPS - lol @ Ghell's sig! :D

ghell
04-15-2004, 11:36 AM
hehe, yea its sposed to be a joke but whammy told me to take it off, I replied asking how much i was allowed but he hasnt answered, i dont know if theres a pm block on him r something though, there was no reply button so i sent him a fresh pm, but this isnt about the thread so ill shh now

btw, i dont know if select case is what he was looking for since he hasnt said anything, but it helped me (for once) hehe :p

M@rco
04-15-2004, 12:04 PM
Glad I was of some help (for once)! :D

And incidentally, the word is "irrelevant"! :p

Fuego
04-15-2004, 04:48 PM
I optimized my code with all of ur tips but I does look like
it isnt speeded up that much.
Writing a Stored Procedure now and thats hard too!

M@rco
04-16-2004, 02:37 AM
Here's a question - how long does the entire While...Wend loop take to execute (save the Timer value either side of the loop and subtract)? If it's not a significant amount of time then you should be concentrating your optimization efforts elsewhere... ;)