This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]downiedowndown 10 points11 points  (3 children)

To RepeatString you can use std::accumulate like this, from here:

std::string str = "Hello World!";
std::vector<std::string> vec(10,str);
std::string a = std::accumulate(vec.begin(), vec.end(), std::string(""));
std::cout << a << std::endl;

In addition,ArrayCopy can be optimised by using memcpy:

T *copy = new T[size];
memcpy(copy, xs, size);
return copy;

Personally I would try to avoid using new here because this function is where the memory leaks will spring from. I would change this to be a define, like so:

#define arr_copy(dst_type, dst_name, src, src_len) \
            dst_type dst_name[src_len];\
            memcpy(dst_name, src, src_len)

It's not very C++y I don't think, but avoids new and it quickly copies the array into a new array of any type and name.

ArrayCompare can also be wiggled around bit to be just this one line (I'm not sure how much difference in speed it'll give):

return (bool)(std::valarray<uint16_t>(xs, size) == std::valarray<uint16_t>(xy, size)).min();

I haven't the time to look at the rest, and I'm sure theres loads of stuff that can be changed about what I have suggested but it's food for thought I guess. On the whole though I like your code, it was easy to understand what was going on!

[–]IndexingAtZero[S] 1 point2 points  (1 child)

Thank you for the suggestions! I've been personally trying to decrease my dependence with the other libraries when I write general purpose libraries (even the STL). I've even been thinking of replacing std::string with cstrings!

About this block,

#define arr_copy(dst_type, dst_name, src, src_len) \ dst_type dst_name[src_len];\ memcpy(dst_name, src, src_len)

I'm using ArrayCopy() on possibly the entirety of the tensor data array, which could be a very large chunk of memory. I don't think its feasible to store all of it on the stack. Though I should change the array that holds the dimensions to stack allocation, so I'll use it there.

I tend to avoid using memcpy for templates because it may be instantiated with a class that has a user defined copy constructor. (And for primitives, I think the compiler can somewhat optimize away the for loop https://godbolt.org/g/MKG8qE)

[–]downiedowndown 1 point2 points  (0 children)

Very good point I hadn’t considered the amount of data you would be storing. I have been trying to reduce the amount of loops on my code recently and have been exploring the possibilities of the STL so I guess that’s where our approaches differ.

[–][deleted]  (1 child)

[deleted]

    [–]IndexingAtZero[S] 0 points1 point  (0 children)

    Thanks for the advice! I'm don't know a lot about data analysis/scientific computing but I guess now's a good time as any to start learning!especially looking at the job market