C / C ++为Python程序员 [英] C/C++ for Python programmer

查看:123
本文介绍了C / C ++为Python程序员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要切换从Pyt​​hon来C / C ++。

你知道一个快速的参考指南或类似的东西要如何启动一个参考?例如像在numpy的和SciPy的教程。

我也读了不少文件的,例如:

I have to switch from Python to C/C++.
Do you know a quick "reference tutorial" or something like that to have a reference to how to start? For example something like the Numpy and Scipy tutorials.
I have read a lot of "documentation", for example


      
  • C ++傻瓜

  •   
  • 的K&R C程序设计语言

  •   
  • 有很多博客和在线文档如:http://eli.thegreenplace.net/2010/01/11/pointers-to-arrays-in-c/,

  •   
  • http://newdata.box.sk/bx/c/

  •   
  • 吨的Q&A在这里计算器的

  •   
  • ...

但它仍然不清楚,我连怎么开始做移植到C / C ++是这样的:

but it's still not clear to me even how to do start porting to C/C++ something like:

#!/usr/bin/env python

import time
import numpy as np
import tables as tb

"""Retrieve 3D positions form 1000 files and store them in one single HDF5 file.
"""

t = time.time()

# Empty array
sample = np.array([])
sample.shape = (0,3)

# Loop over the files
for i in range(0, 1000):
  filename = "mill2sort-"+str(i)+"-extracted.h5"
  print "Doing ", filename
  # Open data file
  h5f = tb.openFile(filename, 'r')
  # Stack new data under previous data
  sample = np.vstack((sample, h5f.root.data.read()))
  h5f.close()

# Create the new file
h5 = tb.openFile("mill2sort-extracted-all", 'w')
# Save the array
h5.createArray(h5.root, 'data', sample, title='mill_2_sub_sample_all')
h5.flush()
h5.close()

print "Done in ", time.time()-t, " seconds."

在C或C ++。在这个例子中我甚至无法理解如何通过一个三维数组一个函数,找到它的尺寸,像

in C or C++. In this example I was not even able to understand how to pass a 3D array to a function that find it's dimensions, something like

int getArrayDimensions(int* array, int *dimensions){
  *dimensions = sizeof(*array)/sizeof(array[0]);
  return 0;
}

使用数组是一个

int array[3][3][3] = ...

感谢您的任何建议!)

推荐答案

好吧,让刚刚用C启动了。

Alright, lets just start with C for now.

void readH5Data(FILE *file, int ***sample);   // this is for you to implement
void writeH5Data(FILE *file, int ***sample);  // this is for you to implement

int main(int argc, const char *argv[])
{
#define width 3
#define height 3
#define depth 3

    time_t t = time(NULL);

    int ***sample = calloc(width, sizeof(*sample));

    for (int i = 0; i < width; i++)
    {
        sample[i] = calloc(height, sizeof(**sample));
        for (int j = 0; j < height; j++)
        {
            sample[i][j] = calloc(depth, sizeof(***sample));
        }
    }

    for (int i = 0; i < 1000; i++)
    {
        char *filename[64];
        sprintf(filename, "mill2sort-%i-extracted.h5", i);

        // open the file
        FILE *filePtr = fopen(filename, "r");

        if (filePtr == NULL || ferror(filePtr))
        {
            fprintf(stderr, "%s\n", strerror(errno));
            exit(EXIT_FAILURE);
        }
        readH5Data(filePtr, sample);

        fclose(filePtr);
    }

    char filename[] = "mill2sort-extracted-all";

    FILE *writeFile = fopen(filename, "w");

    if (writeFile == NULL || ferror(writeFile))
    {
        fprintf(stderr, "%s\n", strerror(errno));
        exit(EXIT_FAILURE);
    }

    writeH5Data(writeFile, sample);

    fflush(writeFile);
    fclose(writeFile);

    printf("Done in %lli seconds\n", (long long int) (time(NULL) - t));

    for (int i = 0; i < width; i++)
    {
        for (int j = 0; j < width; j++)
        {
             free(sample[i][j]);
        }

        free(sample[i]);
    }

    free(sample);
}

只要你记住,你的数组是3x3x3的,你应该没有问题,超越你的'writeH5Data'方法的界限。

As long as you remember that your array is 3x3x3, you should have no problems overstepping the bounds in your 'writeH5Data' method.

这篇关于C / C ++为Python程序员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆