ios - Objective C float vs int, CGPoint vs custom int based struct performance -
based on arguments in post: performance of built-in types, can conclude custom implementation of int based point structure faster or more efficient float-based cgpoint? have reviewed many posts concerning type performance differences have not found 1 includes scenarios further wrapped structure. thanks.
// coord typedef struct { int x; int y; } coord; cg_inline coord coordmake(int x, int y){ coord coord; coord.x = x; coord.y = y; return coord; } cg_inline bool coordequaltocoord(coord coord, coord anothercoord) { return coord.x == anothercoord.x && coord.y == anothercoord.y; } cg_inline cgpoint cgpointforcoord(coord coord) { return cgpointmake(coord.x, coord.y); } edit: have done purely arithmetical tests , results negligible until millions of iterations, application not come close doing. continue use coord typedef remove struct few of reasons @meaning-matters suggests. record tests did show int based structure 30% faster, 30% of 0.0001 seconds not should care about. still interested in points , counter-points on implementation better.
it depends on doing it. ordinary arithmetic, throughput can similar. integer latency bit less. on processors, latency l1 better gprs fpr. so, many tests, results come out same or give small edge integer computation. balance flip other way double vs int64_t computation on 32-bit machines. (if writing cpu vector code , can away 16-bit computation faster use integer.)
however, in case of calculating coordinates/addresses purposes of loading or storing data into/from register, integer better on cpu. reason load or store instruction can take integer operand index array, not floating point one. use floating point coordinates, @ minimum have convert integer first, load or store, should slower. typically, there have rounding mode set (e.g. floor() operation) , maybe non-trivial operation account edging modes, such cl_address_repeat addressing mode. contrast simple , operation, may necessary achieve same thing on integer , should clear integer cheaper format.
on gpus, emphasize floating-point computation bit more , may not invest in integer computation (even though easier), story quite different. there can expect texture unit hardware use floating point value directly find required data. floating point arithmetic find right coordinate built in hardware , therefore "free" (if ignore energy consumption considerations) , graphics apis gl or cl built around it.
generally speaking, though ubiquitous in graphics apis, floating-point numerically inferior choice coordinate system sampled data. lumps precision in 1 corner of image , may cause quantization errors / inconsistencies @ far corners of image, leading reduced precision linear sampling , unexpected rounding effects. large enough images, pixels in image may become unaddressable coordinate system, because no floating-point number exists references position. case default rounding mode, round nearest ties undesirable coordinate systems because linear filtering place coordinate half way between 2 integer values, resulting in round pixels , round down odd. causes pixel duplication rather expected result in worst case hell ways cases , stride 1. nice in easier use.
a fixed-point coordinate system allows consistent coordinate precision , rounding across entire surface , avoid these problems. modulo overflow feeds nicely common edging modes. precision predictable.
Comments
Post a Comment