ハッシュまたは PHP のような配列を実装したいと考えています。キーで要素を検索するには、オプション a) またはオプション b) のどちらが優れていますか?
(すべての変数が設定され、初期化されます!)
a)
for( i = 0; i < ary->element_cnt && found == NULL; i++ ) {
current_element = &(ary->elements[i]);
if( 0 == memcmp(current_element->key, search_key, keysize) ) {
found = current_element;
}
}
b)
for( i = 0, current_element = &(ary->elements[i]) ;
i < ary->element_cnt &&
0 != memcmp(current_element->key, searchkey, keysize);
i++, current_element = &(ary->elements[i]) );
/*found = current_element;*/
最初のほうが読みやすい/保守しやすいので、より良いですか? 2番目の方が速いでしょうか?
すべてを 1 つの大きなループで行うのは「悪いスタイル」ですか?
もっと良い検索アルゴリズムがあることは知っていますが、それは私の問題ではありません!