473,324 Members | 2,370 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Code bloating when choosing template argument based on cmdline input

Hi,

I have two questions.

1. Are there any good way to generalize the code for switch statement
with any number of cases?

2. Suppose test() is a very big function, and there are many cases in
the switch statement. The binary code could be huge. Are there any
method to deal with this problem.

Note: I have to path N as a template argument because the compiler can
optimize test much better than if I pass N as a function argument.

Thanks,
Peng

#include <iostream>
#include <cstdlib>

template <int N>
void test() {
std::cout << N << std::endl;
//Can be a very long function.
}

int main(int argc, char *argv[]) {
int n = atoi(argv[1]);
switch(n) {
case 0: test<0>(); break;
case 1: test<1>(); break;
case 2: test<2>(); break;
case 3: test<3>(); break;
case 4: test<4>(); break;
case 5: test<5>(); break;// Can be much more than 5 cases
default:
std::cout << "not found" << std::endl;
}
}

Jul 3 '07 #1
4 1614
On Jul 3, 9:46 am, "PengYu...@gmail.com" <PengYu...@gmail.comwrote:
Hi,

I have two questions.

1. Are there any good way to generalize the code for switch statement
with any number of cases?
There are good ways and 'good' differs from case to case. Figure out
the commonality and put them in a function. The variations in terms of
inputs to be operated on can be passed as arguments.
2. Suppose test() is a very big function, and there are many cases in
the switch statement. The binary code could be huge. Are there any
method to deal with this problem.
Yes many instantiations of a template can lead to code bloat, the
object code will be huge. Find an alternate way.
example, you have a queue implementation where the element type is a
template param, so long as you do not have many instantiations like
200 odd, its fine. But if you have more than that then change your
design.
Note: I have to path N as a template argument because the compiler can
optimize test much better than if I pass N as a function argument.

Thanks,
Peng

#include <iostream>
#include <cstdlib>

template <int N>
void test() {
std::cout << N << std::endl;
//Can be a very long function.

}

int main(int argc, char *argv[]) {
int n = atoi(argv[1]);
switch(n) {
case 0: test<0>(); break;
case 1: test<1>(); break;
case 2: test<2>(); break;
case 3: test<3>(); break;
case 4: test<4>(); break;
case 5: test<5>(); break;// Can be much more than 5 cases
default:
std::cout << "not found" << std::endl;
}

}
Consider this:
#include <iostream>
#include <cstdlib>
void test(int N) {
std::cout << N << std::endl;
//Can be a very long function.
// I guess you either do not need N here or will be doing some
operations on N.
//
}

int main(int argc, char *argv[]) {
int n = atoi(argv[1]);
switch(n) {
case 0: test(0); break;
case 1: test(1); break;
case 2: test(2); break;
case 3: test(3); break;
case 4: test(4); break;
case 5: test(5); break;// Can be much more than 5 cases
default:
std::cout << "not found" << std::endl;
}
}

HTH
---
Regards,
Taran

Jul 3 '07 #2
On Jul 3, 4:55 am, Taran <taran.tripa...@gmail.comwrote:
On Jul 3, 9:46 am, "PengYu...@gmail.com" <PengYu...@gmail.comwrote:
Hi,
I have two questions.
1. Are there any good way to generalize the code for switch statement
with any number of cases?

There are good ways and 'good' differs from case to case. Figure out
the commonality and put them in a function. The variations in terms of
inputs to be operated on can be passed as arguments.
2. Suppose test() is a very big function, and there are many cases in
the switch statement. The binary code could be huge. Are there any
method to deal with this problem.

Yes many instantiations of a template can lead to code bloat, the
object code will be huge. Find an alternate way.
example, you have a queue implementation where the element type is a
template param, so long as you do not have many instantiations like
200 odd, its fine. But if you have more than that then change your
design.
Note: I have to path N as a template argument because the compiler can
optimize test much better than if I pass N as a function argument.
Thanks,
Peng
#include <iostream>
#include <cstdlib>
template <int N>
void test() {
std::cout << N << std::endl;
//Can be a very long function.
}
int main(int argc, char *argv[]) {
int n = atoi(argv[1]);
switch(n) {
case 0: test<0>(); break;
case 1: test<1>(); break;
case 2: test<2>(); break;
case 3: test<3>(); break;
case 4: test<4>(); break;
case 5: test<5>(); break;// Can be much more than 5 cases
default:
std::cout << "not found" << std::endl;
}
}

Consider this:

#include <iostream>
#include <cstdlib>

void test(int N) {
std::cout << N << std::endl;
//Can be a very long function.
// I guess you either do not need N here or will be doing some
operations on N.
//
}
In fact, I used this approach. But it is not as efficient as the
templated version of "test" function. The runtime difference can be a
few times.

It seems there is no good solution to this problem. I have to use the
templated version of "test", which is not flexible to accept arbitrary
arguments.
int main(int argc, char *argv[]) {
int n = atoi(argv[1]);
switch(n) {
case 0: test(0); break;
case 1: test(1); break;
case 2: test(2); break;
case 3: test(3); break;
case 4: test(4); break;
case 5: test(5); break;// Can be much more than 5 cases
default:
std::cout << "not found" << std::endl;
}
}

HTH
---
Regards,
Taran

Jul 3 '07 #3
Pe*******@gmail.com wrote:
Hi,

I have two questions.

1. Are there any good way to generalize the code for switch statement
with any number of cases?
no. You have to state explicitly the parameters in the template version,
because for each different template parameter set a different class and
different code will be instantiated.
2. Suppose test() is a very big function, and there are many cases in
the switch statement. The binary code could be huge. Are there any
method to deal with this problem.
No. A different code will be generated for each class at compile time.
Note: I have to path N as a template argument because the compiler can
optimize test much better than if I pass N as a function argument.
well, of course, because a lot of choices can be done at compile-time.
Remember, however, that this approach limits by far the flexibility of
the system. The major drawback is that you have to fix all the
parameters at compile time. Otherwise, optimise the code in another way,
usually this can lead to very good results as well.
Thanks,
Peng

#include <iostream>
#include <cstdlib>

template <int N>
void test() {
std::cout << N << std::endl;
//Can be a very long function.
}
In this example, the performance difference should be negligible. Are
you sure that your optimisations depend on the template and you are not
omitting some important optimisation consideration that is not related
with the templates?

Regards,

Zeppe

Jul 3 '07 #4
Pe*******@gmail.com wrote:
Hi,

I have two questions.

1. Are there any good way to generalize the code for switch statement
with any number of cases?
Good? Not really. The template has to be instantiated for all values that
you need. You can ask the compiler to do that, but the compiler will not be
happy:
#include <cstddef>
#include <iostream>

template <int N>
void test() {
std::cout << N << std::endl;
//Can be a very long function.
}

template < typename ArithmeticType,
ArithmeticType first, ArithmeticType last >
void
bin_search ( ArithmeticType val ) {
if ( first == last ) {
test<first>();
return;
}
if ( ( first + ( last-first )/2 ) < val ) {
bin_search< ArithmeticType, ( last - ( last-first )/2), last >( val );
} else {
bin_search< ArithmeticType, first, ( first + ( last-first )/2) >( val );
}
}

int main(int argc, char *argv[]) {
unsigned char n = atoi(argv[1]);
bin_search<unsigned char, 0,-1>( n );
}

Expect your compiler to get mad if you try this for unsigned long instead of
unsigned char.

2. Suppose test() is a very big function, and there are many cases in
the switch statement. The binary code could be huge. Are there any
method to deal with this problem.
Yes: don't make N a template parameter.
Note: I have to path N as a template argument because the compiler can
optimize test much better than if I pass N as a function argument.
Oh well, never mind.
Best

Kai-Uwe Bux
Jul 3 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

67
by: Steven T. Hatton | last post by:
Some people have suggested the desire for code completion and refined edit-time error detection are an indication of incompetence on the part of the programmer who wants such features. ...
8
by: vpadial | last post by:
Hello, I want to build a library to help exporting c++ functions to a scripting languagge. The scripting language provides a function to register functions like: ANY f0() ANY f1(ANY) ANY...
6
by: RainBow | last post by:
Greetings!! I introduced the so-called "thin-template" pattern for controlling the code bloat caused due to template usage. However, one of the functions in the template happens to be virtual...
19
by: jameso321 | last post by:
Hi, We run an MS Access 2000 DB with about 15 users. It is on a Win 2000 Server (SP4) machine and runs through Citrix Metaframe Presentation Server 3.0. --------------------------
171
by: tshad | last post by:
I am just trying to decide whether to split my code and uses code behind. I did it with one of my pages and found it was quite a bit of trouble. I know that most people (and books and articles)...
14
by: Gary Wessle | last post by:
Hi I wrote this routine below but getting the error below it which I don't know how to fix. thanks //useful.h /**
7
by: Bo Yang | last post by:
Hi , I am reading some boost code now , and I got confused by the following code in the add_reference type trait . template <class TT&(* is_reference_helper1(wrap<T>) )(wrap<T>); char...
7
by: gretean | last post by:
I have a problem that's driving me crazy involving Microsoft's ability to deduce template parameters. I am using Visual Studio .NET (aka VC7?), and it gives an error compiling the following code....
25
by: David Sanders | last post by:
Hi, As part of a simulation program, I have several different model classes, ModelAA, ModelBB, etc., which are all derived from the class BasicModel by inheritance. model to use, for example...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.