Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
WITH object
Message
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Title:
Miscellaneous
Thread ID:
01671871
Message ID:
01671889
Views:
70
>>I haven't tested, but would like to know what This is inside a With-Endwith construct. IOW
>THIS will still be scoped as it would be outside of the WITH statement - ie. scoped to the parent class that contains this code.
>+++ Rick ---

It's an interesting thing. Passing in "this" as a parameter, or using an implicit parameter by encapsulating the call in a "WITH this"? You definitely gain some performance with the implicit parameter pass:
* Results on my machine:
* Parameter     -- 33.049 seconds
* Implicit WITH -- 21.240 seconds
* Demonstrates a performance boost by not passing in the parameter, and referencing the contextual WITH environment


PUBLIC gnTot
CLEAR
lo = CREATEOBJECT("thetest")
lo.test()


* test1() receives an input parameter
FUNCTION test1
LPARAMETERS toObj
    gnTot = gnTot + toObj.val


* test2() receives an implicit environment with a contextual WITH in scope
FUNCTION test2
    gnTot = gnTot + .val


DEFINE CLASS thetest AS Custom
    val = 5

    PROCEDURE test
    LOCAL lnI, lfStart1, lfEnd1, lfStart2, lfEnd2
        * Pass by parameter
        gnTot = 0
        lfStart1 = SECONDS()
        FOR lnI = 1 TO 10000000
            test1(this)
            test1(this)
            test1(this)
            test1(this)
            test1(this)
        NEXT
        lfEnd1 = SECONDS()
        ? "Result passing by parameter", gnTot

        * No parameter pass, using WITH
        WITH this
            gnTot = 0
            lfStart2 = SECONDS()
            FOR lnI = 1 TO 10000000
                test2()
                test2()
                test2()
                test2()
                test2()
            NEXT
            lfEnd2 = SECONDS()
            ? "Result passing by implicit WITH this", gnTot
            ? ""
        ENDWITH

        ? ""
        ? "Parameter time (in seconds):", lfEnd1 - lfStart1
        ? "Implicit WITH time:", lfEnd2 - lfStart2

ENDDEFINE
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform