testregex.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "fspr_strings.h"
  17. #include "fspr_pools.h"
  18. #include "fspr_general.h"
  19. #include "fspr_hash.h"
  20. #include "fspr_lib.h"
  21. #include "fspr_time.h"
  22. #include <regex.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. int main( int argc, char** argv) {
  26. fspr_pool_t *context;
  27. regex_t regex;
  28. int rc;
  29. int i;
  30. int iters;
  31. fspr_time_t now;
  32. fspr_time_t end;
  33. fspr_hash_t *h;
  34. if (argc !=4 ) {
  35. fprintf(stderr, "Usage %s match string #iterations\n",argv[0]);
  36. return -1;
  37. }
  38. iters = atoi( argv[3]);
  39. fspr_initialize() ;
  40. atexit(fspr_terminate);
  41. if (fspr_pool_create(&context, NULL) != APR_SUCCESS) {
  42. fprintf(stderr, "Something went wrong\n");
  43. exit(-1);
  44. }
  45. rc = regcomp( &regex, argv[1], REG_EXTENDED|REG_NOSUB);
  46. if (rc) {
  47. char errbuf[2000];
  48. regerror(rc, &regex,errbuf,2000);
  49. fprintf(stderr,"Couldn't compile regex ;(\n%s\n ",errbuf);
  50. return -1;
  51. }
  52. if ( regexec( &regex, argv[2], 0, NULL,0) == 0 ) {
  53. fprintf(stderr,"Match\n");
  54. }
  55. else {
  56. fprintf(stderr,"No Match\n");
  57. }
  58. now = fspr_time_now();
  59. for (i=0;i<iters;i++) {
  60. regexec( &regex, argv[2], 0, NULL,0) ;
  61. }
  62. end=fspr_time_now();
  63. puts(fspr_psprintf( context, "Time to run %d regex's %8lld\n",iters,end-now));
  64. h = fspr_hash_make( context);
  65. for (i=0;i<70;i++) {
  66. fspr_hash_set(h,fspr_psprintf(context, "%dkey",i),APR_HASH_KEY_STRING,"1");
  67. }
  68. now = fspr_time_now();
  69. for (i=0;i<iters;i++) {
  70. fspr_hash_get( h, argv[2], APR_HASH_KEY_STRING);
  71. }
  72. end=fspr_time_now();
  73. puts(fspr_psprintf( context, "Time to run %d hash (no find)'s %8lld\n",iters,end-now));
  74. fspr_hash_set(h, argv[2],APR_HASH_KEY_STRING,"1");
  75. now = fspr_time_now();
  76. for (i=0;i<iters;i++) {
  77. fspr_hash_get( h, argv[2], APR_HASH_KEY_STRING);
  78. }
  79. end=fspr_time_now();
  80. puts(fspr_psprintf( context, "Time to run %d hash (find)'s %8lld\n",iters,end-now));
  81. return 0;
  82. }