Serving the Quantitative Finance Community

 
User avatar
GM
Topic Author
Posts: 0
Joined: August 20th, 2002, 5:11 pm

VB functions

March 25th, 2003, 3:41 pm

Hi,I am switching to a VB platform and am finding that when I try to call application. functions from excel it's incredibly slow. I guess this is because the excel/VBA function library is so large it takes a while to search through it (each time) for the relevant function.Are there any publicly available libraries of VB functions? Nothing too complex is needed (I guess I could program my own but I am lazy), just the useful stuff like normsdist, minv, and other simple functions would be handy.Thanks,GM
 
User avatar
Flex
Posts: 2
Joined: December 19th, 2002, 9:32 am

VB functions

March 25th, 2003, 3:52 pm

Try WorksheetFunction.normsist, maybe this is faster.hth, Flex
 
User avatar
GM
Topic Author
Posts: 0
Joined: August 20th, 2002, 5:11 pm

VB functions

March 26th, 2003, 9:17 am

This doesn't seem to help too much.e.g. I call the Max function 10,000 times.Application.Max takes 14 seconds (!)WorksheetFunction.Max takes 6 secondsExcel.Application.Max takes 5 secondsUsing my own Max function is instantaneous.Any ideas??GM
 
User avatar
jens
Posts: 0
Joined: July 14th, 2002, 3:00 am

VB functions

March 26th, 2003, 9:20 am

Which data type are you using?
 
User avatar
Boofta
Posts: 0
Joined: July 14th, 2002, 3:00 am

VB functions

March 26th, 2003, 9:43 am

Can I assume that you are using Excel as an out-of-process (.EXE) server for your VB program?If so, you are going to see huge performance degradation (3/4 orders of magnitude?) compared to running native VB code. Most of the time is spent marshalling data between client & server processes.If you need to use the Excel functions for some unavoidable reason, build an in-process COM component in VB that early binds to Excel, then invokve this COM component via VBA. In that way, your VB code is running in Excel's process with early binding, which cuts-out the cross-process marshalling overhead.But if you are already of aware this, then pretend I didn't reply.RegardsBoof
 
User avatar
GM
Topic Author
Posts: 0
Joined: August 20th, 2002, 5:11 pm

VB functions

March 26th, 2003, 1:13 pm

Boof,No, I wasn't aware of that. It sounds like the most likely explanation. I was surprised that it's not clever enough to call the function once and then keep it cached for the remainder of the run. The degradation in speed is at least x10.I don't have to use the excel functions but it would save me some time to do so. As I'm not all that techy could you tell me what a COM is and how easy it is to apply (sample code if you have any would be ideal)? I'm not clear from your explanation whether the code runs from VBA or from VB initially.I used to do everything in VBA and am only switching to VB for speed purposes, it seems I may have been too hasty! I guess I will have to build up my own library of functions if there are no public libraries available on the internet.Jens,The data type isn't relevant, I'm just doing max(0,1) in a loop.Thanks for your replies,GM
 
User avatar
afoster
Posts: 5
Joined: July 14th, 2002, 3:00 am

VB functions

March 26th, 2003, 2:40 pm

A good COM tutorial can be found at Developmentor/VBCOM Essentially though, every component you create in VB is COM (ActiveX) compliant. The link above will explain all this much more thoroughly though. Also, VB is essentially VBA (especially if you were using VBA in Office XP) with a few more libraries. You can check this in Project>References and see what components are automatically referenced by your project. Now back to the Automation problems.To early bind to Excel, you need to first reference the component, which I assume you are already doing, then declare a variable as shown below; ie do not use "as Object" or "as Variant". You can now either set this variable to a New instance of Excel, or use GetObject to reference an existing instance of Excel ie;Dim xl as Excel.ApplicationDim n as IntegerSet xl = New Excel.AppliocationFor n = 0 to 9999 xl.WorksheetFunction.Max 5, 4Nextxl.QuitSet xl = NothingA full explaination can be found at MSDN Automation ExplainationWith regards to execution speed, however, I would certainly write my own Math function library in an "in-process" component (read ActiveX Dll type project) You can always reuse this, and, if you install your app on someone elses machine that uses a different version of excel, you will not get any automation errors due to mismatched type libraries. Hope this helps