我们想从NameValueCollection中删除几个键,但我们不确定它们是否确实存在于其中。
如果我尝试删除不在NameValueCollection中的key1,则没有异常/副作用:
#include<stdio.h>
#include<math.h>
#include<omp.h>
#define END 10000000
int is_prime( long long v ) // Only pass odd values to this function
{ long long end = sqrt( (double) v );
for ( long long i = 3;
i <= end;
i += 2 ) {
if ( ( v % i ) == 0 ) return 0;
}
return 1;
}
int main( int argc, char **argv )
{
#define OMP_NUM_OF_THREADS 4 // CONFIG:
// [SEQ]
__uint64_t global_per_thread_bracket[ OMP_NUM_OF_THREADS]; // :--
__uint64_t global_per_thread_sub_count[OMP_NUM_OF_THREADS]; // :--
#pragma omp parallel num_threads( OMP_NUM_OF_THREADS ) // :-- [PAR]x4--------------------------||||-4-threads
{ // {|...} ||||-4-threads
int thread_local_ID = omp_get_thread_num();// {|...} // SET: _id ||||-4-threads
int thread_local_COUNT = 0; // {|...} // SET: SAFE ZERO ||||-4-threads
__uint64_t thread_local_BRACKET = 10; // {|...} // SET: SAFE 10 ||||-4-threads
// {|...} // ||||-4-threads
printf( "INF(t:%3d)|||: _id\n", thread_local_ID ); // PRINT:?(ORD) ||||-4-threads
// {|...} // ||||-4-threads
for ( long long i = ( 3 + ( 2 * thread_local_ID ) ); // CALC:---------// ||||-4-threads
i < END; // {|...} // // ||||-4-threads
i += ( 2 * OMP_NUM_OF_THREADS ) // {|...} // // ||||-4-threads
) // {|...} // // ||||-4-threads
{ if ( i > thread_local_BRACKET ) // {|...} // IF:local // ||||-4-threads
{ printf( "|||(t:%3d)|||:%12d\t%12d\n", thread_local_ID, // PRINT:?(ORD) // ||||-4-threads
(int)thread_local_BRACKET, // {|...} // local // ||||-4-threads
(int)thread_local_COUNT // {|...} // local // ||||-4-threads
); // {|...} // // ||||-4-threads
thread_local_BRACKET *= (__uint64_t)10; // {|...} // UPDATE:*=10 // ||||-4-threads
} // {|...} // // ||||-4-threads
if ( is_prime(i) ) thread_local_COUNT++; // {|...} // IF:local++ // ||||-4-threads
} // ------------------------------------------------------------{|...}----------------------// ||||-4-threads
global_per_thread_sub_count[ thread_local_ID] = thread_local_COUNT; // ||||-4-threads
global_per_thread_bracket[ thread_local_ID] = thread_local_BRACKET;// ||||-4-threads
} // ---------------------------------------------------------//_________{|___}_________________________||||-4-threads
long long prime_count = 1; // :-- SET:=1 as 2 is prime
for ( int thread_OrdNo = 0; // :-- CONSOLIDATE: -----//
thread_OrdNo < OMP_NUM_OF_THREADS; // :-- //
thread_OrdNo++ // :-- //
) // :-- //
{ prime_count+= global_per_thread_sub_count[thread_OrdNo]; // :-- UPDATE: += //
printf( ":--(t:%3d):--:%12d\t%12d\n",(int)thread_OrdNo, // :-- PRINT: //
(int)global_per_thread_bracket[ thread_OrdNo], // :-- //
(int)global_per_thread_sub_count[thread_OrdNo] // :-- //
); // :-- //
} // --------------------------------------------------------------:-- -------------------//
printf( "[END] PRIME COUNT was omp.FOUND == %9lld <= %lld\n", // :--
prime_count, // :--
END // :--
); // :--
}
但是这样做的理想方法是,我们应该在删除之前检查密钥是否存在吗?
答案 0 :(得分:5)
答案是否定的。在尝试删除密钥之前,您无需检查密钥。 如果密钥不存在,则不会抛出异常。
我相信你不应该这样做,因为它会导致O(n)* 2操作。