Page Index Toggle Pages: 1 [2]  Send Topic Send Topic Print Print
Very Hot Topic (More than 25 Replies) RunExitProgrammingOnCloseForm(state) ? (Read 3010 times)
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: RunExitProgrammingOnCloseForm(state) ?
Reply #15 - Jan 31st, 2012 at 9:41pm
Print Post Print Post  
lksseven wrote on Jan 31st, 2012 at 9:32pm:
loop from 1 to 1... I couldn't figure out a better way to find the oldest date in the result set, and the newest date in the result set, other than to sort in ascend order and then just loop once to grab the first record only(oldest/smallest date), then resort in descend order and again loop once to grab the first record only (newest/biggest date).


If you only want one record, set the result set position and get the value you want. Say you have a result set:

Code
Select All
vCnt = @XResultSetTotal(vRS)
XResultSetCurrentPosition(vRS, 1)
vMinDate = @XResultSetValue(vRS, "OrderDate")
XResultSetCurrentPosition(vRS, vCnt)
vMaxDate = @XResultSetValue(vRS, "OrderDate")
 



Quote:
What's worrisome to me is that the admonishment that I've been backhanded made me laugh instead of grimace (kind of an Animal House fraternity paddle induction ceremony "thank you, sir, may I have another" thing).

Yeah...I get that a lot.   Wink

Quote:
Regarding taking 90 XRset searches down to one? If you can do that I'll start my own 'Hammer' religion.

See the code in my previous post. That's what it does. You're too late to start said religion, but you can join. For offerings, I accept cash, major credit cards, and cake. Mainly cake.
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
lksseven
Full Member
***
Offline



Posts: 416
Location: Southwest
Joined: Jan 26th, 2009
Re: RunExitProgrammingOnCloseForm(state) ?
Reply #16 - Jan 31st, 2012 at 11:20pm
Print Post Print Post  
I have looked over your suggested technique, and understand some of the methodology behind it. 

I will puzzle over it this evening and report back.

  

Larry
Back to top
IP Logged
 
Carl Underwood
Senior Member
Members
*****
Offline



Posts: 1351
Location: New Hampshire
Joined: Mar 11th, 2003
Re: RunExitProgrammingOnCloseForm(state) ?
Reply #17 - Feb 1st, 2012 at 4:32am
Print Post Print Post  
Erika,

I noticed that your code had comments about not sorting to save time.

I did a bit of testing on this quite a while ago, and discovered that sorting before using @UniqueStringArray will always result in a faster process. And especially if you are going to do both, it's much faster to sort first, then run the unique function. My notes indicate that I found it to be 6 times faster than doing it the other way around.

Just to be sure this would still be true in this case, I tested it again on this example. I increased the number of customer records to over 10,000 in order to make it take longer than 1 second to process. Then, I ran this (minus the line that sorts) slightly modified snippet from your example. It took my system 10-11 seconds. After that, I ran it again with the sort, and it took just 6-8 seconds.
Code
Select All
var vRS as Int
var vState as String
var vLoop as Int
var vCnt as Int
var vStateList as String
var vDate as Date
var vStart as Int

vStart = @TimeInSeconds()	// Added by Carl

	vDate = @Date

	// Get list of unique States as a StringArray
	// We don't need them to be in order, so we're
	// not gonna waste time on sorting
	vStateList = ""
	vRS = @XResultSetSearch(@FN, "Customers", 0, 2, "")
	If vRS > -1
	{
		vCnt = @XResultSetTotal(vRS)
		For vLoop = 1 To vCnt
			XResultSetCurrentPosition(vRS, vLoop)
			vState = @XResultSetValue(vRS, "State")
			vStateList = vStateList + vState + ";"
		Next
		XResultSetClose(vRS)
	}
	vStateList = @Repllas(vStateList, ";", "")
	vStateList = @SortStringArray(vStateList, 0)	// Added by Carl
	vStateList = @UniqueStringArray(vStateList)

WriteLn(@TimeInSeconds() - vStart)	// Added by Carl 



So, even if you don't need the string to be sorted, I find it's always better to use it anyway, because it helps the unique process run faster.
  


Carl Underwood
CDU Computer Consulting LLC
Epsom, New Hampshire
Back to top
IP Logged
 
Acebanner
Full Member
***
Offline



Posts: 123
Location: New York City
Joined: Jan 23rd, 2008
Re: RunExitProgrammingOnCloseForm(state) ?
Reply #18 - Feb 1st, 2012 at 12:32pm
Print Post Print Post  
Very interesting discussion folks! Stuff like this is why I visit the forum daily.
  
Back to top
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: RunExitProgrammingOnCloseForm(state) ?
Reply #19 - Feb 1st, 2012 at 2:13pm
Print Post Print Post  
That's good tip,Carl. I forgot about that.

In this particular case, you might try timing it with the sort on the XResultSet instead of on the string array. It's likely to sort faster and would still let you get the speed increase from running Unique on a presorted list. You never know until you try it though. That's why optimization is often so non-intuitive.
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
lksseven
Full Member
***
Offline



Posts: 416
Location: Southwest
Joined: Jan 26th, 2009
Re: RunExitProgrammingOnCloseForm(state) ?
Reply #20 - Feb 1st, 2012 at 4:12pm
Print Post Print Post  
OK ... great news, and a lingering snag ...

Great news first:

Hammer, the speed increase is beyond belief - I went from a 2 min calc using my original code, down to 6 seconds using your code.  Whatever flavor of cake you want, it's yours.

Lingering snag:

I tried to faithfully follow Hammer's suggested coding, but the code isn't getting, for each ItemNum, the correct QtyTotal and # of Recs (I don't completely yet understand the logic trail of the program, so I'm sure I've mis-stepped somewhere).     I'm going to paste the code, and then show a snippet of the program output versus the actual numbers (in red):

First the code:

SUBROUTINE CalcAndEmit()

var vAvgInterval as int
var vAvgQty as int
var vPredictedDate as date

     vAvgInterval = 0
     vAvgQty = 0
     vPredictedDate = vDate

     If vRecs > 1
       {

           vAvgInterval = @DefinedRound((@ToNumber(vMaxSODate) - @ToNumber(vMinSODate)) / vRecs, 0, 1)

           vPredictedDate = vMaxSODate + vAvgInterval
           vAvgQty = @DefinedRound(vQtyTotal / vRecs, 0, 1)
           If vAvgQty < 1
             {
                 vAvgQty = 1
             }
           If (vPredictedDate > (vDate - 7)) and (vPredictedDate < (vDate + 7))
           //If (1 = 1)      // Forcing output since none of the sample dates are current
               {
                           //WriteLn("Reorder Date for " + vAvgQty + " " + vItemUom + " of ItemNum #" + vItemNum + " / " + vItemDesc + @NewLine() + " should be " + vPredictedDate + @NewLine() + @NewLine())
                         Writeln("ItemNum # = " + vItemNum + "; vRecs = " + vRecs + ", vQtyTotal = " + vQtyTotal + ", vMaxSODate = " + vMaxSODate + ", vMinSODate = " + vMinSODate + "; vSONum = " + vSONum + @NewLine())
                  vNeedSoonAddTo = ""
                  vNeedSoonAddTo = "Reorder Date for " + vAvgQty + " " + vItemUoM + " of ItemNum #" + vItemNum + " / " + vItemDesc + @NewLine() + " should be " + vPredictedDate + @NewLine() + @NewLine()
                  vNeedSoonFYI = vNeedSoonFYI + vNeedSoonAddTo + @NewLine()
               }
       }

END SUBROUTINE

     // Get list of unique ItemNums as a StringArray
     // We don't need them to be in order, so we're
     // not gonna waste time on sorting


     vItemNumList = ""
     vRS = @XResultSetSearch(@FN, "Orders!SOLines", SEARCH_MODE_AND, SEARCH_SYNTAX_QA, "!ShipToClientID=" + vClientID, "!SODate=" + vDateRange, "!ItemCat=/" + "DESC;LBR;RC", "!SONum=/" + "..a;..b;..c;..d;..e;..x")
     If vRS > -1
       {
           vCnt = @XResultSetTotal(vRS)
           For vLoop = 1 To vCnt
                 XResultSetCurrentPosition(vRS, vLoop)
                 vItemNum = @XResultSetValue(vRS, "ItemNum")
                 vItemNumList = vItemNumList + vItemNum + ";"
           Next
     XResultSetClose(vRS)
       }

     vItemNumList = @Repllas(vItemNumList, ";", "")
     vItemNumList = @SortStringArray(vItemNumList, 0)
     vItemNumList = @UniqueStringArray(vItemNumList)

writeln(vItemNumList)

     // Get calc info for all the ItemNums at once instead of doing
     // one @XResultSetSearch for each ItemNum by using the list assembled
     // above as search criteria.

     vRS = @XResultSetSearch(@FN, "Orders!SOLines", SEARCH_MODE_AND, SEARCH_SYNTAX_QA, "!ShipToClientID=" + vClientID, "!ItemNum=" + vItemNumList, "!SODate=" + vDateRange, "!ItemCat=/" + "DESC;LBR;RC", "!SONum=/" + "..a;..b;..c;..d;..e;..x")   // , "!Company=/=")
     If vRS > -1
       {
           // Now the sort order matters
           XResultSetSort(vRS, "ItemNum:-1;SODate:-1")
           vCnt = @XResultSetTotal(vRS)

writeln("vCnt - " + vCnt)

           vUniqueItemNum = "This will never appear as an ItemNum"
           vQtyTotal = 0
           vRecs = 0
           vLoop = 1
           // Calc each ItemNum
           For vLoop = 1 To vCnt
                 XResultSetCurrentPosition(vRS, vLoop)
                 vItemNum = @XResultSetValue(vRS, "ItemNum")
                 vItemDesc = @XResultSetValue(vRS, "ItemDesc")
                 vItemUoM = @XResultSetValue(vRS, "ItemUoM")
                 //vSONum = @XResultSetValue(vRS, "SONum")

                 If vUniqueItemNum <> vItemNum
                 {
                       If vUniqueItemNum <> "This will never appear as an ItemNum"
                       {

                             CalcAndEmit()      // Emit result

                       }
                       // Reset for next ItemNum
                       vUniqueItemNum = vItemNum
                       vMinSODate = @ToDate(@XResultSetValue(vRS, "SODate"))
                       vMaxSODate = vMinSODate
                       vQtyTotal = 0
                       vRecs = 0
                                               
                 }
                 
                 vMaxSODate = @XResultSetValue(vRS, "SODate")
                 vItemQty = @XResultSetValue(vRS, "ItemQtyShadow")
                 vQtyTotal = vQtyTotal + vItemQty
                 vRecs = vRecs + 1
                 
           Next
           XResultSetClose(vRS)
           CalcAndEmit()      // Emit last ItemNum


       }
     XResultSetClose(vRS)

Now the resultant output from the code (with actual numbers inserted in red :

ItemNum # = 1-5513G; vRecs = 140, vQtyTotal = 383, vMaxSODate = 2012/01/27, vMinSODate = 2011/08/05
      actual: 
ItemNum      ItemQtyShadow
1-5513G      2.00
1-5513G      2.00
1-5513G      1.00
1-5513G      2.00
1-5513G      2.00
1-5513G      1.00
1-5513G      1.00
1-5513G      2.00
8      13.00


ItemNum # = 1-5517; vRecs = 39, vQtyTotal = 54, vMaxSODate = 2012/01/27, vMinSODate = 2011/08/05
actual:

ItemNum      ItemQtyShadow
1-5517      0.60
1-5517      1.00
1-5517      1.00
1-5517      0.60
4      3.20


ItemNum # = 54-0281081001; vRecs = 26, vQtyTotal = 120, vMaxSODate = 2012/01/27, vMinSODate = 2011/08/05
actual:

ItemNum      ItemQtyShadow
54-0281081001      1.00
54-0281081001      2.00
54-0281081001      (1.00)
54-0281081001      1.00
54-0281081001      1.00
54-0281081001      1.00
54-0281081001      1.00
7      6.00


ItemNum # = 54-0281136001; vRecs = 7, vQtyTotal = 6, vMaxSODate = 2012/01/12, vMinSODate = 2011/09/23
actual:

ItemNum      ItemQtyShadow
54-0281136001      1.00
1      1.00




  

Larry
Back to top
IP Logged
 
Carl Underwood
Senior Member
Members
*****
Offline



Posts: 1351
Location: New Hampshire
Joined: Mar 11th, 2003
Re: RunExitProgrammingOnCloseForm(state) ?
Reply #21 - Feb 1st, 2012 at 7:10pm
Print Post Print Post  
Hammer wrote on Feb 1st, 2012 at 2:13pm:
That's good tip,Carl. I forgot about that.

In this particular case, you might try timing it with the sort on the XResultSet instead of on the string array. It's likely to sort faster and would still let you get the speed increase from running Unique on a presorted list. You never know until you try it though. That's why optimization is often so non-intuitive.


Ahh, good idea, so I tested that too.

I would have thought that it would sort faster with XResultSetSort(vRS, "State:-1") instead of sorting the string array, but the times were identical -- 7 seconds either way.
  


Carl Underwood
CDU Computer Consulting LLC
Epsom, New Hampshire
Back to top
IP Logged
 
lksseven
Full Member
***
Offline



Posts: 416
Location: Southwest
Joined: Jan 26th, 2009
Re: RunExitProgrammingOnCloseForm(state) ?
Reply #22 - Feb 1st, 2012 at 7:18pm
Print Post Print Post  
To try and narrow down the field of vision, I limited the ItemNums retrieved to just two ItemNums
(here is the code:
     vRS = @XResultSetSearch(@FN, "Orders!SOLines", SEARCH_MODE_AND, SEARCH_SYNTAX_QA, "!ShipToClientID=" + vClientID, "!SODate=" + vDateRange, "!ItemCat=/" + "DESC;LBR;RC", "!SONum=/" + "..a;..b;..c;..d;..e;..x", "!ItemNum=" + "1-5513G;1-5511G"))

Then I ran a writeln in the subroutineto see the result of each loop run (here is the writeln code):            Writeln("ItemNum # = " + vItemNum + "; vRecs = " + vRecs + ", vQtyTotal = " + vQtyTotal + ", vMaxSODate = " + vMaxSODate + ", vMinSODate = " + vMinSODate + "; vSONum = " + vSONum + @NewLine())
           If (vPredictedDate > (vDate - 7)) and (vPredictedDate < (vDate + 7))
           

Here is the result:
1-5511G;1-5513G
vCnt - 148
ItemNum # = 1-5513G; vRecs = 140, vQtyTotal = 383, vMaxSODate = 2012/01/27, vMinSODate = 2011/08/05; vSONum =
this itemNum should be 1-5511G (the vRecs and vQtyTotal is correct for 1-5511G)

ItemNum # = 1-5513G; vRecs = 140, vQtyTotal = 383, vMaxSODate = 2012/01/27, vMinSODate = 2011/08/05; vSONum =
this line shouldn't be there (appears to be a duplicate as far as I can tell)

ItemNum # = 1-5513G; vRecs = 8, vQtyTotal = 13, vMaxSODate = 2012/01/20, vMinSODate = 2011/08/05; vSONum =
this line is correct.

It's not listing the first ItemNum (1-5511G), but is getting the vRecs (140) and QtyTotal (383) for 1-5511G, but then attaching those results to the second ItemNum (1-5513G). 
Then the last subroutine 'emit' is accurate (the 1-5513G for 8 records and 13 total quantity)

  

Larry
Back to top
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: RunExitProgrammingOnCloseForm(state) ?
Reply #23 - Feb 1st, 2012 at 9:28pm
Print Post Print Post  
In CalcAndEmit, use vUniqueItemNum instead of vItemNum.
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
lksseven
Full Member
***
Offline



Posts: 416
Location: Southwest
Joined: Jan 26th, 2009
Re: RunExitProgrammingOnCloseForm(state) ?
Reply #24 - Feb 1st, 2012 at 10:47pm
Print Post Print Post  
Bingo!

My wife was so amazed at me jumping around and hollering after your final tip enabled complete success (from 2 minutes to 5.5 seconds!) that she said I had her permission to marry you.

Oh, man!, this will open up a wealth of possibilities.

Thank you thank you thank you.
  

Larry
Back to top
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: RunExitProgrammingOnCloseForm(state) ?
Reply #25 - Feb 1st, 2012 at 11:30pm
Print Post Print Post  
lksseven wrote on Feb 1st, 2012 at 10:47pm:
My wife was so amazed at me jumping around and hollering after your final tip enabled complete success (from 2 minutes to 5.5 seconds!) that she said I had her permission to marry you.



Wedding cake!!!!!
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
lksseven
Full Member
***
Offline



Posts: 416
Location: Southwest
Joined: Jan 26th, 2009
Re: RunExitProgrammingOnCloseForm(state) ?
Reply #26 - Feb 2nd, 2012 at 3:35am
Print Post Print Post  
"Wedding Cake!!!"

LOL - we're both laughing out loud at that.  Too funny.
  

Larry
Back to top
IP Logged
 
tcgeo
Full Member
***
Offline



Posts: 278
Location: Traverse City, Michigan
Joined: May 13th, 2008
Re: RunExitProgrammingOnCloseForm(state) ?
Reply #27 - Feb 2nd, 2012 at 10:46am
Print Post Print Post  
Just curious, does it make any difference time wise using; SEARCH_MODE_AND, SEARCH_SYNTAX_QA, versus, 0,2,?
  
Back to top
IP Logged
 
The Cow
YaBB Administrator
*****
Offline



Posts: 2530
Joined: Nov 22nd, 2002
Re: RunExitProgrammingOnCloseForm(state) ?
Reply #28 - Feb 2nd, 2012 at 1:42pm
Print Post Print Post  
tcgeo wrote on Feb 2nd, 2012 at 10:46am:
Just curious, does it make any difference time wise using; SEARCH_MODE_AND, SEARCH_SYNTAX_QA, versus, 0,2,?


A smalll increase in compile time. No differenece at all in run time.
  

Mark Lasersohn&&Programmer&&Lantica Software, LLC
Back to top
IP Logged
 
Page Index Toggle Pages: 1 [2] 
Send Topic Send Topic Print Print