Example #1
0
int main(int argc, char *argv[])
{
	PYTABLE *pt;
	char str_gbk[BUFSIZ], str_py[BUFSIZ];
	char str_utf[BUFSIZ] = "王雨晨";

	// Convert to GBK
	m_iconv("UTF-8", str_utf, strlen(str_utf), "GBK", str_gbk, sizeof str_gbk);

	// py_open
	if ((pt = py_open("table/gbk.gb")) == NULL) {
		fprintf(stderr, "error: can not open pinyin table\n");
		return 1;
	}

	// get Pinyin by string
	{
		int nconv;
		char *inbuf, *outbuf;
		size_t insize, outsize;
	
		inbuf = str_gbk, insize = strlen(str_gbk);
		outbuf = str_py, outsize = sizeof str_py;
printf("------------------------------------------------\n");
printf(" in: %p, %lu\nout: %p, %lu\n", inbuf, insize, outbuf, outsize);
		nconv = py_convstr(&inbuf, &insize, &outbuf, &outsize, PY_UPPER_I/* | PY_INITIAL*/, pt);
		if (nconv == (size_t) -1) {
			error(0, 0, "py_convstr: outbuf too small");
			return -1;
		}
printf(" in: %p, %lu\nout: %p, %lu\n", inbuf, insize, outbuf, outsize);
printf("nconv: %ld\n", (long) nconv);
printf("------------------------------------------------\n");
		m_iconv("GBK", str_py, strlen(str_py), "UTF-8", str_utf, sizeof str_utf);
printf("%s\n", str_utf);
	}

	// py_close
	py_close(pt);

	return 0;
}
Example #2
0
int main(int ac, const char** av)
{
  const struct test_desc tests[] =
  {
    /* TEST_DESC(hi), */
    /* TEST_DESC(hix), */
    TEST_DESC(matmul),
    /* TEST_DESC(arr), */
    TEST_DESC_INVALID
  };

  static const size_t n = 10000;

  size_t i;
  size_t j;
  uint64_t ticks[2];
  double us[2];

  if (py_init())
  {
    printf("py_init() error\n");
    return -1;
  }

  for (i = 0; tests[i].name != NULL; ++i)
  {
    const struct test_desc* const t = &tests[i];
    py_handle_t py;

    printf("---");
    printf("--- %s test\n", t->name);

    if (py_open(&py))
    {
      printf("py_open error\n");
      continue ;
    }

    if (py_compile(&py, t->py_str))
    {
      printf("py_compile error\n");
      goto on_close;
    }

    /* add variables */
    if (t->post_compile(&py))
    {
      printf("py_post_compile error\n");
      goto on_close;
    }

    /* execute python version */
    ticks[0] = rdtsc();
    for (j = 0; j != n; ++j)
    {
      /* if (j && ((j % 100) == 0)) py_collect(&py); */

      t->pre_exec(&py);

      if (py_execute(&py))
      {
	printf("py_execute error (%zu)\n", j);
	goto on_close;
      }
    }
    ticks[1] = rdtsc();
    us[0] = (double)ticks_to_us(sub_ticks(ticks[0], ticks[1])) / 1000000.0;

    py_print_out_vars(&py);

    /* execute c version */
    ticks[0] = rdtsc();
    for (j = 0; j != n; ++j)
    {
      t->pre_exec(&py);
      t->c(&py);
    }
    ticks[1] = rdtsc();
    us[1] = (double)ticks_to_us(sub_ticks(ticks[0], ticks[1])) / 1000000.0;

    py_print_out_vars(&py);

    printf("py = %lfus, c = %lfus\n", us[0], us[1]);

  on_close:
    py_close(&py);
  }

  py_fini();

  return 0;
}