0

I know simular questions have been asked before but I believe my specifics vary enough that this cam be it's own question. If I've missed another question that's very simular please link it for me.

I'm proficient in vb.net but a total novice at c++. I've got a subroutine in vb.net that just does a lot of maths. There's some trigonometry and squareroots etc. nothing too demanding but it's around 100 equations and they have to be run thousands of times.

Here's some pseudo code of what I do:

Sub mathsub()
For i = 0 to arraylength
A = somenum(i) * othernum(i)
...
Next
End sub

So I do all the math inside the for loop many thousands of times. Each time uses a different element of a bunch of arrays (somenum, othernum, etc.), all of these arrays are the same length.

No single line of code takes any significant length of time (when profiled each line takes a negligible length of time), but all in all the program takes longer than I'd like. Can I expect a significant speed increase by rewriting this routeen in c++ and referencing it from vb.net?

Edit #1 - answers to questions in comments

The code takes a negligible amount of time per array element but there's about 20,000 elements and I need to do this about 10,000 times (so 200 million times - no joke). All in I've estimated it taking around 18 hours based on how long it takes for a fraction of the work. The entire program will need to be run multiple times as well, although each new run is 100% separate an unrelated. I have tried threading the code, I have a Ryzen 1800x 8 core (16 thread) processor - I've tried splitting the task into exactly 15 thread (leaving 1 thread for overhead and other activities) and using a parallel.for loop and both don't offer enough of an increase yet. I don't know what AVX is, but I'll start looking into it, I've never written a line of C++, but the maths itself is all just simple equations and I'm happy to spend a few days/weeks/months learning implementing if it's seriously going to make a difference!

FraserOfSmeg
  • 1,128
  • 2
  • 23
  • 41
  • How long does the VB code take to execute? Even if you were to get a significant improvement, if the VB code executes quickly enough then it really isn't worth the effort. For instance, a 50% increase in performance would be important if something takes an hour, could be important if something takes a minute but is likely not worth the effort if something takes a second. – jmcilhinney Nov 08 '18 at 07:27
  • The trig and math isn't going to be any faster, they are intrinsics in vb.net. Might actually [be slower](https://stackoverflow.com/a/687741/17034). But you might well be ahead with the array manipulations, a C++ compiler doesn't generate bounds checks by default and might be able to auto-vectorize and auto-parallelize the code. – Hans Passant Nov 08 '18 at 08:06
  • "I'm proficient in vb.net but a total novice at c++" probably means that you will write better vb than you would c++ ... it might be fun as a learning project though – sudo rm -rf slash Nov 08 '18 at 09:13
  • 1
    Threading might be more relevant, ad that's available from vb.net as well. For serious speedups, you'd have to also use AVX, but that's not a realistic goal if you're a C++ novice. – MSalters Nov 08 '18 at 09:29
  • I've answered the questions from comments in my edit to the question. Thank you all for your input, I hope my answers help explain my problem a little more! – FraserOfSmeg Nov 08 '18 at 20:17

1 Answers1

0

Benchmark it. There's no way to know that without benchmarking, but the answer is likely to be "yes" because of C++'s performance profile versus a managed language like VB.NET, but JIT optimizations, managed-to-native transitions, array access order / data locality, and general coding technique can all have an impact here.