#include "xmq.h"
#include
#include
#include
#define ROUNDS 10000
XMQProceed add_value(XMQDoc *doc, XMQNode *node, void *user_data)
{
int *sum = (int*)user_data;
*sum += xmqGetIntRel(doc, ".", node);
return XMQ_CONTINUE;
}
int main()
{
XMQReturnDoc rd = xmqNewDoc();
if (rd.status != XMQ_OK) return 1;
XMQDoc *ixml = rd.doc;
bool ok = xmqParseBufferWithType(ixml,
"decode = -'a', B++-','. B=[N]+.",
NULL, NULL, XMQ_CONTENT_IXML, 0);
if (!ok)
{
printf("Grammar error\n%s", xmqDocError(ixml));
return 1;
}
struct timeval start, stop;
gettimeofday(&start, NULL);
int sum = 0;
for (int i = 0; i < ROUNDS; ++i)
{
rd = xmqNewDoc();
if (rd.status != XMQ_OK) return 1;
XMQDoc *decode = rd.doc;
ok = xmqParseBufferWithIXML(decode, "a123,2,444", NULL, ixml, 0);
if (!ok)
{
printf("Parse error\n%s", xmqDocError(decode));
return 1;
}
sum = 0;
xmqForeach(decode, "//B", add_value, &sum);
xmqFreeDoc(decode);
}
gettimeofday(&stop, NULL);
xmqFreeDoc(ixml);
double us = (stop.tv_sec - start.tv_sec) * 1000000.0 + (stop.tv_usec - start.tv_usec);
printf("sum of a123,2,444 is %d\n", sum);
printf("each ixml parse and foreach took %d us\n", (int)(us / ROUNDS));
return 0;
}