Saving Drupal CCK node, drupal_execute is a dog, use node_save instead

Interesting benchmark, saving 100 CCK nodes in a loop, nearly 5 seconds difference between drupal_execute method and going straight for node_save, i guess this indicates how much overhead is in the form handling (which is not a bad thing!)

heres the output!

time node object based insert: 0.00971524078067 each average, total 0.981767416
time node drupal_execute based insert: 0.0454754971042 each average, total 5.57557964325


  for($i=0; $i<=100; $i++) {
    $node = new stdClass();
    $node->is_new=1;
    $node->title = "Book node".rand();
    $node->body = "";
    $node->type = "book";
    $node->uid = 1;
    $node->teaser = "";
    $node->filter = 1;
    $node->status = 1;
    $node->comment = 2;
    $node->created = time();
    $node->changed = time();
    $node->field_publisher[0]['value']='Gutenberg';
    $node->field_author[0]['value']='Gutenberg';
    $now = microtime(true);
    node_save($node);
    $x=$x+microtime(true)-$now;
    $t=$t+microtime(true)-$now;
  }
  
  $x=$x/$i;
  
  
  print "time node object based insert: $x each average, total $t
\n"; $x=0; for($i=0; $i<=100; $i++) { $form_state = array(); module_load_include('inc', 'node', 'node.pages'); // new for Drupal 6 $nodeTmp = array('type' => 'book'); // a variable holding the content type $form_state['values']['type'] = 'book'; // the type of the node to be created $form_state['values']['status'] = 1; // set the node's status to Published, or set to 0 for unpublished $form_state['values']['title'] = 'Test Book'.rand(); // the node's title $form_state['values']['op'] = t('Save'); // this seems to be a required value $form_state['values']['name'] = 'admin'; $form_state['values']['field_publisher'][0]['value'] = 'Gutenberg'; // CCK text field example for CCK in D6 $form_state['values']['field_author'][0]['value'] = 'Leigh'; $now = microtime(true); $err = drupal_execute('book_node_form', $form_state, (object) $nodeTmp); $x=$x+microtime(true)-$now; $t=$t+microtime(true)-$now; } $x=$x/$i; print "time node drupal_execute based insert: $x each average, total $t
\n";