레이블이 malloc 2d array contiguous인 게시물을 표시합니다. 모든 게시물 표시
레이블이 malloc 2d array contiguous인 게시물을 표시합니다. 모든 게시물 표시

12/12/2015

[swift] 2D-array is continuos




[Abstract]

 In c/c++ programming, 2D-array is massively used to deal with the large and practical data as like images, video frames, or any kind of scientific ones. When accessing this array, it is too well-introduced to use double-for-statement, but my goal here is to refresh that the array itself in the physical memory space is just continuous.

 Thus in certain cases, it reduces the processing time to make use of this feature. In brief, eliminate one of the for-statement from the double-shell, and make it to calculate the address inside the for-statement to reduce the overhead of iterative assembly-branch-instructions.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <stdio.h>
#include <iostream>
#include <atlstr.h>
#include <math.h>
using namespace std;
 
int main(void)
{
#define ROWS 10
#define COLS 10
    
    double data[ROWS][COLS];    // data[row][col]
    double *pData = data[0];    // data[row] is pointer!!
 
    register int row, col;        // generic index for 2D-array
    register int rows = sizeof(data) / sizeof(data[0]);
    register int cols = sizeof(data[0]) / sizeof(data[0][0]);
    
    // file opened
    FILE* fin = fopen("[0] data.txt""r");
    if(!fin) return -1;
    cout << endl << endl;
 
    //------------------------------------------------------------------------
    // case[1] : generic method : double for statement
    //------------------------------------------------------------------------
    for (row = 0; row < rows; ++row)
    {
        for (col = 0; col < cols; ++col)
        {
            fscanf(fin, "%lf"&data[row][col]); // data[row][col] is value !!
            printf("%10.2lf ", data[row][col]);
        }
        cout << endl;
    }
    //------------------------------------------------------------------------
 
    cout << endl << endl;
    fclose(fin);
 
    fin = fopen("[0] data.txt""r");
    if (!fin) return -1;
 
    //------------------------------------------------------------------------
    // case[2] : continuos in memory space
    //------------------------------------------------------------------------    
    register int i;
    for (i = 0; i < rows * cols; ++i)
    {
        fscanf(fin, "%lf", pData + i);
        printf("%10.2lf "*(pData + i));
        if (i % cols == cols - 1cout << endl;
    }
    //------------------------------------------------------------------------
 
    cout << endl << endl;
    fclose(fin);
    
    return 0;
}
cs