objective c - Check if any values have changed a certain amount -
does know efficient ways check if set of integers contains integer has changed amount.
for example have:
int1 = 10; int2 = 20; int3 = 30;
and want know if of these 3 integers change 30
now if int1
becomes 40
call should triggered. @ first thought of doing this.
if (abs((int1+int2+int3)-(newint1+newint2+newint3)) >= 30) {
but many problems can arise this...
- false triggers (e.g. each new int value increases 10, making net change greater 30 not individual int greater 30)
- untriggered reactions (e.g. 1 of new integer values has increased 50 should called new integer value decreased 50 (again, should called) net change 0 because
-50+50=0
)
does have efficient way of doing this? (yes, know check each value individually or statements...)
so far best stab @
if ((((abs(int1-newint1))>=30)+((abs(int2-newint2))>=30)+((abs(int3-newint3))>=30))>0) {
but that's basically same using or statement (probably takes little longer or statment.
i don't think can faster that, , unless you're dealing hundreds of millions of integers, should not introduce significant performance penalty.
however, might want "clever". if somehow "checksum" 2 sums? example, multiply old , new numbers nth prime, check if difference of new , old sum divided index
th prime amount want.
int sum(int arr[], size_t n) { int n = 0; (int = 0; < n; i++) n += primes[i] * arr[i]; return n; } int primes[3] = { 2, 3, 5 }; // or more int olds[3] = { 10, 20, 30 }; int news[3] = { 40, 20, 30 }; int nth = 0; // check first int change_expected = 30; int oldsum = sum(olds, 3); int newsum = sum(news, 3); if ((newsum - oldsum) / primes[nth] == change_expected) { // 1st value changed expected }
note take way more time , cpu cycles naive approach.
Comments
Post a Comment