Advertisement
aeris

C (un-)efficiency…

May 9th, 2013
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. Problem : having one file created on the current dir, want to create it on /tmp
  2. Solution : prepend « /tmp/ » to the filename, without breaking some other code part…
  3.  
  4. Language 1 : Java, 10ms
  5. Before :
  6. String filename;
  7. … some code to calculate the filename …
  8. File file = new File(filename);
  9.  
  10. After :
  11. String filename;
  12. … some code to calculate the filename …
  13. File file = new File("/tmp/" + filename); # Or better, new File("/tmp", filename) !
  14.  
  15. Langage 2 : C, 10min
  16. Before :
  17. char filename[255];
  18. … some code to calculate the filename …
  19. FILE *file = fopen(filename, "rb");
  20.  
  21. After :
  22. char filename[255];
  23. … some code to calculate the filename …
  24. File *file = fopen(filename, "rb");
  25. char tmp[255]; # Sniff, hardcoded length, and one useless variable :(…
  26. strcat(tmp, "/tmp/");
  27. strcat(tmp, filename);
  28. strcpy(filename, tmp);
  29. FILE *file = fopen(filename, "rb");
  30.  
  31. No comment…
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement