Change offsetVolume function
## What this patch does to fix the issue.
This PR will improve the performance.
And this PR also make the logic simpler a little.
## Link to any relevant issues or pull requests.
Fix
@@ -66,12 +66,14 @@ int Tensor::shapeVolume() {
}
int Tensor::offsetVolume(const std::vector<int>& indices) const {
- int offset = 0, size = data_.size();
- int i = 0;
- for (auto itr = indices.begin(); itr != indices.end(); ++itr, ++i) {
- size /= shape_[i];
- offset += (*itr) * size;
+ int offset = 0;
+ int size = 1;
+
+ for (int i = shape_.size() - 1; i >= 0; --i) {
+ offset += indices[i] * size;
+ size *= shape_[i];
}
+
return offset;
}
