BUFR performance: strcpy/strcat faster than sprintf
@@ -132,7 +132,10 @@ static int next_attribute(bufr_keys_iterator* kiter)
return 0;
}
prefix=(char*)grib_context_malloc_clear(kiter->current->context,strlen(kiter->prefix)+strlen(kiter->attributes[i_curr_attribute]->name)+3);
- sprintf(prefix,"%s->%s",kiter->prefix,kiter->attributes[i_curr_attribute]->name);
+ /*sprintf(prefix,"%s->%s",kiter->prefix,kiter->attributes[i_curr_attribute]->name);*/
+ strcpy(prefix, kiter->prefix); /* strcpy and strcat here are much faster than sprintf */
+ strcat(prefix, "->");
+ strcat(prefix, kiter->attributes[i_curr_attribute]->name);
grib_context_free(kiter->current->context,kiter->prefix);
kiter->prefix=prefix;
kiter->attributes=kiter->attributes[i_curr_attribute]->attributes;
@@ -143,6 +146,10 @@ static int next_attribute(bufr_keys_iterator* kiter)
int codes_bufr_keys_iterator_next(bufr_keys_iterator* kiter)
{
+ /* ECC-734: de-allocate last key name stored */
+ grib_context_free(kiter->handle->context, kiter->key_name);
+ kiter->key_name = NULL;
+
if(kiter->at_start)
{
kiter->current = kiter->handle->root->block->first;
@@ -169,9 +176,6 @@ int codes_bufr_keys_iterator_next(bufr_keys_iterator* kiter)
while(kiter->current && skip(kiter))
kiter->current = grib_next_accessor(kiter->current);
- /* ECC-734: de-allocate last name created */
- if(kiter->key_name) grib_context_free(kiter->handle->context, kiter->key_name);
-
return kiter->current != NULL;
}
@@ -187,7 +191,10 @@ char* codes_bufr_keys_iterator_get_name(bufr_keys_iterator* kiter)
if (kiter->prefix) {
int iattribute=kiter->i_curr_attribute-1;
ret=(char*)grib_context_malloc_clear(c,strlen(kiter->prefix)+strlen(kiter->attributes[iattribute]->name)+10);
- sprintf(ret,"%s->%s",kiter->prefix,kiter->attributes[iattribute]->name);
+ /*sprintf(ret,"%s->%s",kiter->prefix,kiter->attributes[iattribute]->name);*/
+ strcpy(ret, kiter->prefix); /* strcpy and strcat here are much faster than sprintf */
+ strcat(ret, "->");
+ strcat(ret, kiter->attributes[iattribute]->name);
} else {
ret=(char*)grib_context_malloc_clear(c,strlen(kiter->current->name)+10);
@@ -195,7 +202,7 @@ char* codes_bufr_keys_iterator_get_name(bufr_keys_iterator* kiter)
r=(int*)grib_trie_get(kiter->seen,kiter->current->name);
sprintf(ret,"#%d#%s",*r,kiter->current->name);
} else {
- sprintf(ret,"%s",kiter->current->name);
+ strcpy(ret,kiter->current->name);
}
}
