[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 345
  • Last Modified:

Port code to VBA

I need an opinion.................
Can the C++ code be ported to VBA?
I am not asking someone to write the code... can it be ported or is it necessary
to create a dll in C++ and use the dll in MS Access.
If it could be rewritten in VBA, what would be a reasonable number of hours?

The code is lines snipped from several  sub/functions...
This is public domain code
Interesting story, This is the fastest open source 7-card poker hand evaluator.
Look it up, in a very large data table/array. Something like 30mil hands a second.

Thanks for your help



Is it possible for these lines/snips of code C++ to be written in VBA
 
 
              int HR[32487834];
 
	memset(HR, 0, sizeof(HR));
	FILE * fin = fopen("HandRanks.dat", "rb");
	if (!fin)
	    return false;
	size_t bytesread = fread(HR, sizeof(HR), 1, fin);	// get the HandRank Array
	fclose(fin);
 
 
	int cards[] = { 2, 6, 12, 14, 23, 26, 29 };
	int retVal = LookupHand(cards);
 
 
	int LookupHand(int* pCards)
	{
	    int p = HR[53 + *pCards++];
	    p = HR[p + *pCards++];
	    p = HR[p + *pCards++];
	    p = HR[p + *pCards++];
	    p = HR[p + *pCards++];
	    p = HR[p + *pCards++];
          	    return HR[p + *pCards++];
	}

Open in new window

0
RMB1791
Asked:
RMB1791
  • 4
  • 2
  • 2
1 Solution
 
Patrick MatthewsCommented:
Hello RMB1791,

The short is yes, almost certainly it could be written in VBA.  I only say "almost certainly" because since
I am not a C++ guy I do not completely understand the code you posted, but I'd be shocked if VBA just
could not do it at all.  Comment that code, and I can give a more definitive answer.

That said, VBA is **slow** compared to any exe or dll you might create from C++, VB6, or the .Net
varieties thereof.  Glacially slow in comparison.  So, if speed is of the essence, stay away from VBA.

Regards,

Patrick
0
 
RMB1791Author Commented:
Thanks Patrick,

Speed is a major issue, the purpose of the code is to do real-time calcuations while playing.
I have 5-10 seconds each turn of cards, to answer  fold/call/raise.
The snips are complete as to the operating lines of code. Balance is consol printing ect.
It is my understanding that memset ..... is not possible in VB/VBA also Array pointers may not
be possible?
I want to contract to have it ported. 1) Its possible? 2) Whats reasonable?
If necessary to go dll route 1) Faster? 2) Wrap it and simplify the connection?

Attached complete code from original, it is set to loop thru all 24mil possible hands.
My interest is in a simgle hand.





TwoPlusTwo-HandEval.txt
0
 
Jim Dettman (Microsoft MVP/ EE MVE)PresidentCommented:
I agree with Patrick; most certainly it could be done in VBA.  Logic is after all logic.  It might not be as efficent as C++ in terms of number of lines of code, but it certainly can be done.
  However as he said, you will get no where the same type of performance.  Since speed is a major issue, I would put in a DLL (not a COM object; too slow) and call the DLL from VB.
FWIW,
JimD.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Jim Dettman (Microsoft MVP/ EE MVE)PresidentCommented:
RMB,
I basically just seconded what Patrick had already said, so I'm not sure why you selected my comment as the accepted solution. Was that a misake? If so, we can get the question re-opened.
JimD.
0
 
RMB1791Author Commented:
im kind of new to this......
Just wanted to say... ok, and close the issue....

Both of you answered to direct question, neither of you gave any analysis to the
code that I presented. How slow is slow? The idea is to suck into memory a data
set that is 200mb, 24mil lines.... I dont care how long it takes to load, only how long
it takes to run 1-2mil calculation s (lookups).



0
 
Jim Dettman (Microsoft MVP/ EE MVE)PresidentCommented:
<<im kind of new to this......
Just wanted to say... ok, and close the issue....>>
OK.  Well generally you give the "accepted solution" to the first person that answers the question and can if you wish, flag other comments as being helpful, which will then receive an "assisted solution".  This is done with the "split" button.
  The only time you don't do that is if a latter comment makes the problem significantly clearer to you or provides a lot more information.  For example, a comment of "go here and read this" may not be as helpful as a detailed explanation, followed by " and you can read this if you need more details".
I happen to be a PE for the MS Access topic area, so I will re-open the question.  Normally you would open a question in community support to get that done if you did make a mistake.
<<Both of you answered to direct question, neither of you gave any analysis to the
code that I presented. How slow is slow? The idea is to suck into memory a data
set that is 200mb, 24mil lines.... I dont care how long it takes to load, only how long
it takes to run 1-2mil calculation s (lookups).>>
  That's because until you actually do it, your not going to know how slow it is. There are just too many variables involved (processor, speed, amount of memory, etc). What can be said with certainty is that since VBA is an incrementally compiled language, it will be at least an order of a magnitude slower that C++ (and probably more then that), which is fully compiled.
  So instead of millions of hands per second, I would think hundreds of thousands at best.  Then depending on how efficient the code is, that might even drop into the thousands per second.
  My best guess would be that you might get a hundred thousand per second, but not much more then that.
JimD.
0
 
Patrick MatthewsCommented:
RMB1791,

With respect, Jim and I answered your question.  In my opinion, a split between the two of us would have been
a fair outcome.

Your direct question was "can it be ported to VBA", and Jim and I answered that (yes).  We also gave you an
opinion about speed: VBA will be much slower, so keeping it in a compiled binary is the way to go based on
the use case you described.  How slow is slow?  VBA would be--at best--at least an order of magnitude slower than
a C++ based DLL.  It could well be even slower than that.

Regards,

Patrick
0
 
Jim Dettman (Microsoft MVP/ EE MVE)PresidentCommented:
<<I happen to be a PE for the MS Access topic area, so I will re-open the question.  Normally you would open a question in community support to get that done if you did make a mistake.>>
Actually, you just need to click on "request attention" by the main question.  I keep forgetting that they added that.  When you do that, you just type a comment and then a mod will look at it and do whatever needs to be done.
JimD.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 4
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now