Estimating printing cost of RFC documents

Suppose that you own an old inkjet printer and want to print the contents of many RFC documents. Could you estimate your printing cost in advance? A new toner cartridge costs you 120$ and is sufficient for printing 10000 pages. Assume that a page can hold 500 words with an average length of 5.2 characters. Using a simple Python script, you extract the title, pages, words and chars of each RFC document (see rfc_data.txt), having a meta information of its number of pages.

Then you could write the following code to find the number of documents to be printed, the total number of pages, the printing cost, the toner cartridges to be used (estimated based on the number of characters to be printed) and the potential environmental impact in terms of trees cut, using information you find on the web.

#include <iostream> #include <vector> #include <tuple> #include <cmath> #include <iomanip> using namespace std; struct Tree { int in_sheets_of_paper = 8333; // }; struct Paper { int pages = 500; double price = 6.5; }; struct Toner { double price = 120.0; int pages = 10000; int words_per_page = 500; double average_word_length = 5.2; int chars_capacity; double dollar_per_char; }; struct Document { string title; int pages; int words; int chars; }; class Printer { private: vector<Document> documents; Toner toner; long pages_printed; long chars_printed; public: Printer(const vector<Document>& docs, Toner t): documents(docs), toner(t) {}; long find_pages_printed() { long pages = 0; for(const Document& doc : documents) { pages += doc.pages; } this->pages_printed = pages; return pages; }; int estimate_cartridges_used() { return (int) ceil(this->chars_printed / toner.chars_capacity); }; double estimate_toner_cost() { long total_chars = 0; for(const Document& doc : documents) { total_chars += doc.chars; } this->chars_printed = total_chars; return total_chars * toner.dollar_per_char; } double estimate_paper_cost() { Paper paper; return (this->pages_printed / (double) paper.pages) * paper.price; } double estimate_total_print_cost() { return this->estimate_toner_cost() + this->estimate_paper_cost(); } int estimate_trees_cut_environmental_effect() { Tree tree; return ceil(this->pages_printed / tree.in_sheets_of_paper); }; }; int main() { Toner toner; toner.chars_capacity = toner.pages * toner.words_per_page * toner.average_word_length; toner.dollar_per_char = toner.price / toner.chars_capacity; vector<Document> docs = { /* paste the contents from rfc_data.txt here, omitted for clarity */ }; Printer printer(docs, toner); cout << "Total RFCs printed: " << docs.size() << endl << "Total pages printed: " << printer.find_pages_printed() << endl << "Toner cost: $" << setprecision(7) << printer.estimate_toner_cost() << endl << "Paper cost: $" << setprecision(7) << printer.estimate_paper_cost() << endl << "Total printing cost: $" << setprecision(7) << printer.estimate_total_print_cost() << endl << "Toner cartridges used: " << printer.estimate_cartridges_used() << endl << "Environmental effect (trees cut): " << printer.estimate_trees_cut_environmental_effect() << endl; // Total RFCs printed: 7859 // Total pages printed: 194498 // Toner cost: $1246.066 // Paper cost: $2528.474 // Total printing cost: $3774.54 // Toner cartridges used: 10 // Environmental effect (trees cut): 23 return 0; }

It is strange how 194498 pages could translate only in 10 toner cartridges used. This could be due to many factors: manufacturers giving optimistic toner cartridge capacity estimates, our (incorrect) assumption of 500 words of 5.2 chars each and others. At the same time, we need to consider that many pages in the RFC documents aren't filled entirely—a lot of white space may be left, where we assume that it is not correlated with toner use (which again might be wrong). Printing costs here are comprised of the estimates of both toner and paper costs, where the costs of the printer itself and any other maintenance costs arising during the printing process are not included.

We also cannot neglect the potential environmental impact, which is equivalent to cutting 23 trees per print per person. If only 50 people printed that much (or maybe even less than that), according to the estimates, this would "consume" over 1000 trees! In reality, even when not perfect, estimates tend to improve over time while using the same process repeatedly.

We could go further and compute cost per page printed, shipping cost (given the number of pages and their weight) and others.

This example illustrates doing work that languages are good at. Python is used to prepare the data in the appropriate format, while C++ handles the actual computations.