For a paper, I have to present two code snippets that do the same thing to compare them.
I started having them in two different figures, but now, I'm trying to have them side by side, and that's where the trouble starts.
First of all, I couldn't seem to get listings to work in subfig.
\lstset{%
language=C,
frame=single,
numbers=left,
numberstyle=\footnotesize,
tabsize=2,
keepspaces=true,
columns=fullflexible,
basicstyle=\ttfamily,
keywordstyle=\color{blue}
}
\begin{figure}
\subfloat[small code]{%
\begin{lstlisting}
for(int i = 0 ; i < N ; i++)
for(int j = 0 ; j < N ; j++)
for(int k = 0 ; k < N ; k++)
c[i][j] += a[i][k] * b[k][j];
\end{lstlisting}
}
\hfill
\subfloat[Larger code]{%
\begin{lstlisting}
for(int ii = 0 ; ii < N ; ii+=B) {
for(int jj = 0 ; jj < N ; jj+=B) {
for(int kk = 0 ; kk < N ; kk+=B) {
for(int i = ii ; i < ii + B ; i++) {
for(int k = kk ; k < kk + B ; k++){
c_i = c[i];
a_ik = a[i][k];
b_k = b[k];
for(int j = jj ; j < jj + B ; j+=2){
c_i[j] += a_ik * b_k[j];
c_i[j+1] += a_ik * b_k[j+1];
}
}
}
}
}
}
\end{lstlisting}
}
\end{figure}
This, generates a score of errors that I can't make sense of. After browsing the web, I found that a lot of people seem to achieve what I want to do by including the source code from an external file.
\begin{figure}[!htb]
\subfloat[Toto]{%
\lstinputlisting{mm_base.c}%
}
\hfill
\subfloat[Tata]{%
\lstinputlisting{mm_opt.c}%
}
\end{figure}
By manually changing the fontsize, I can get the subfloats to be side by side, but I get the following result:
The frame around the code and the perceived size of the content don't seem to match. Moreover, the code on the right hand side goes out of bound, the right frame goes off-page.
The first thing I'd like to know, out of curiosity really, is why I can't use the lstlisting environment and only the lstinputlisting when inside a subfloat.
And the more pressing matters, getting the frame to match the subfig's width. And the more challenging part maybe, automatically change the size of the code included to fit in the subfig.