3
$\begingroup$

I have found that running the following WSTP code, using C++ (MSVS 2010, Windows 7, 32-bit) and Mathematica 11.1, takes about 0.8 Second:

clock_t t0 = clock();
WSPutFunction(link, "EvaluatePacket", 1L);
    WSPutFunction(link, "1", 0); // (Simply evaluating the number 1.)
    while(WSNextPacket(link) != RETURNPKT)
        WSNewPacket(link);
WSEndPacket(link);
clock_t t1 = clock();
double t = (double)(t1 - t0) / CLOCKS_PER_SEC;

On the line where simply the number 1 is evaluated, we wish to run a piece of Mathematica code, which takes less than 0.1 Second when running directly from Mathematica.

If even simply evaluating a constant number takes 0.8 Second, how can we expect to make the overall time taken less than 0.1 Second?

Does anyone know a way out of this problem? Thanks.

UPDATE on Thursday, May 04, 2017:

Complete code:

#include <iostream>
#include "wstp.h"
#include <time.h>
using std::cin; using std::cout; using std::endl;
static void init_and_open_link(int argc, char **argv);
WSENV env = (WSENV)0; WSLINK link = (WSLINK)0;
int main(int argc, char **argv){
    init_and_open_link(argc, argv);
    clock_t t0 = clock();
    WSPutFunction(link, "EvaluatePacket", 1L);
        WSPutFunction(link, "1", 0);
        while(WSNextPacket(link) != RETURNPKT)
            WSNewPacket(link);
    WSEndPacket(link);
    clock_t t1 = clock();
    double t = (double)(t1 - t0) / CLOCKS_PER_SEC;
    cout << "t = " << t << " seconds." << endl;
    WSPutFunction(link, "Exit", 0);
    return 0;}
static void deinit(void){if(env) WSDeinitialize(env);}
static void close_link(void){if(link) WSClose(link);}
static void init_and_open_link(int argc, char **argv){
    int error; env = WSInitialize((WSParametersPointer)0);
    if(env == (WSENV)0) exit(1);
    atexit(deinit);
    link = WSOpenArgcArgv(env, argc, argv, &error);
    if(link == (WSLINK)0) exit(2);
    atexit(close_link);}
$\endgroup$
  • $\begingroup$ Can you give a complete example, please? I would like to see the context of this code, preferably something I can compile without additions and modification, and test. In my experience, such things are not nearly this slow. $\endgroup$ – Szabolcs May 4 '17 at 9:07
  • $\begingroup$ If you have not seen it yes, look at this tutorial: edenwaith.com/development/tutorials/mathlink/ML_Tut.pdf I do not think that WSPutFunction(link, "1", 0); is correct syntax. PutSymbol is used for putting the head of an expression as a symbol. 1 is not a valid symbol name. Maybe you meant WSPutInteger. I do not know what would happen if you do this. In the best case it puts 1[] (which is not 1), in the worst cases it messes up your link. $\endgroup$ – Szabolcs May 4 '17 at 9:09
  • $\begingroup$ If you really want to send 1[], I believe you have to start with WSPutType. $\endgroup$ – Szabolcs May 4 '17 at 9:14
  • $\begingroup$ @Szabolcs, I have just now added the complete code. I have seen the tutorial which you have suggested. Maybe you are right in suggesting WSPutInteger instead of WSPutFunction. But right now, I am in the process of transitioning to a different computer, and it will take a while for me to try out your suggestion. BTW, I just now removed an extra space after "::" in "using std:: endl;" -- not sure whether that mattered. Thanks. $\endgroup$ – math-student May 4 '17 at 18:13
  • $\begingroup$ Using MSVS 2015 C++ (in 64-bit Win 10, with GPU) and Mathematica 11.1.1, the above computation took about 0.4 sec. However, replacing "WSPutFunction(link, "1", 0)" by "WSPutInteger(link, 1)" did NOT make any significant difference. I have tried various combinations by running a release build, and by using the release build of wstp64i4.dll. (BTW, just as I had thought, the space after "::" did not matter at all. Since I am still rather new to C++, I had not been sure about that.) Has anyone been able to run the above code in < 0.4 sec? Thanks. $\endgroup$ – math-student May 23 '17 at 0:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.